module Kiso::ColorUtils
def contrast_text_color(hex, threshold: nil)
-
(String)- "white" or "black"
Parameters:
-
threshold(Float, nil) -- luminance threshold override; defaults to -
hex(String) -- a hex color string, 3-digit (#abc) or 6-digit (#aabbcc)
def contrast_text_color(hex, threshold: nil) threshold ||= Kiso.config.contrast_threshold hex = hex.delete("#") hex = hex.chars.map { |c| c * 2 }.join if hex.length == 3 r, g, b = hex.scan(/../).map { |c| c.to_i(16) / 255.0 } luminance = [r, g, b].map { |c| (c <= 0.04045) ? c / 12.92 : ((c + 0.055) / 1.055)**2.4 }.then { |lr, lg, lb| 0.2126 * lr + 0.7152 * lg + 0.0722 * lb } (luminance > threshold) ? "black" : "white" end