問題:給定目標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中文網其他相關文章!