问题:给定目标 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中文网其他相关文章!