首頁 > web前端 > js教程 > 主體

如何使用 CSS 濾鏡重新著色黑色以匹配任何 RGB 顏色?

Mary-Kate Olsen
發布: 2024-10-19 14:07:30
原創
488 人瀏覽過

How to Recolor Black Using CSS Filters to Match Any RGB Color?

如何僅使用CSS 濾鏡將黑色轉換為任何給定顏色

問題:給定目標RGB 顏色,如何重新著色黑色( #000) 僅使用CSS 濾鏡轉換為該顏色?

解決方案: 使用以下函數將目標 RGB 顏色轉換為 CSS 濾鏡字串:

<code class="python">def css_filter(target_color):
  """Converts a target RGB color into a CSS filter string.

  Args:
    target_color: A tuple of three integers representing the target RGB color.

  Returns:
    A CSS filter string that transforms black into the target color.
  """

  # Convert the target RGB color to HSL.
  h, s, l = colorsys.rgb_to_hls(*target_color)

  # Calculate the CSS filter values.
  invert = 1 - (l / 100)
  sepia = 1 - s
  saturate = s / 100
  hue_rotate = h * 360
  brightness = l
  contrast = 1

  # Return the CSS filter string.
  return "filter: invert({0}%) sepia({1}%) saturate({2}%) hue-rotate({3}deg) brightness({4}%) contrast({5});".format(
      invert, sepia, saturate, hue_rotate, brightness, contrast)</code>
登入後複製

以上是如何使用 CSS 濾鏡重新著色黑色以匹配任何 RGB 顏色?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!