Ruby method to determine if a string would look more readable on black vs. white.
1 2 def self.ideal_font_color_for(hex_str) 3 hex_str = hex_str.split('#').last 4 5 threshold = 105 6 7 r = hex_str[0..1].hex 8 g = hex_str[2..3].hex 9 b = hex_str[4..5].hex 10 11 delta = (r*0.299) + (g*0.587) + (b*0.114) 12 13 255 - delta < threshold ? "#000000" : "#FFFFFF" 14 end