首页  >  文章  >  web前端  >  如何使用 CSS 滤镜重新着色黑色以匹配任何 RGB 颜色?

如何使用 CSS 滤镜重新着色黑色以匹配任何 RGB 颜色?

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-10-19 14:07:30491浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn