Class: Dvl::Color::Generator
- Inherits:
-
Object
- Object
- Dvl::Color::Generator
- Defined in:
- lib/dvl/color/generator.rb
Constant Summary collapse
- DARK_BACKGROUND_CUTOFF =
120
Instance Attribute Summary collapse
-
#background_color ⇒ Object
Returns the value of attribute background_color.
-
#dropdown_background ⇒ Object
Returns the value of attribute dropdown_background.
-
#dropdown_color ⇒ Object
Returns the value of attribute dropdown_color.
-
#input_background ⇒ Object
Returns the value of attribute input_background.
-
#input_background_focus ⇒ Object
Returns the value of attribute input_background_focus.
-
#input_color ⇒ Object
Returns the value of attribute input_color.
Instance Method Summary collapse
- #base_color ⇒ Object
- #black ⇒ Object
- #conservative_background_dark? ⇒ Boolean
- #conservative_background_light? ⇒ Boolean
- #error_bubble_background ⇒ Object
- #error_bubble_color ⇒ Object
- #error_color ⇒ Object
- #generate_dropdown_colors ⇒ Object
-
#initialize(background_color) ⇒ Generator
constructor
A new instance of Generator.
- #target_contrast_ratio ⇒ Object
- #to_debug_s ⇒ Object
- #to_h ⇒ Object
- #to_scss ⇒ Object
- #white ⇒ Object
Constructor Details
#initialize(background_color) ⇒ Generator
Returns a new instance of Generator.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dvl/color/generator.rb', line 12 def initialize(background_color) self.background_color = background_color unless background_color.is_a?(Chroma::Color) self.background_color = Chroma.paint(background_color) end self.input_background, self.input_background_focus, self.input_color = generate_input_colors self.dropdown_background, self.dropdown_color = generate_dropdown_colors end |
Instance Attribute Details
#background_color ⇒ Object
Returns the value of attribute background_color.
4 5 6 |
# File 'lib/dvl/color/generator.rb', line 4 def background_color @background_color end |
#dropdown_background ⇒ Object
Returns the value of attribute dropdown_background.
4 5 6 |
# File 'lib/dvl/color/generator.rb', line 4 def dropdown_background @dropdown_background end |
#dropdown_color ⇒ Object
Returns the value of attribute dropdown_color.
4 5 6 |
# File 'lib/dvl/color/generator.rb', line 4 def dropdown_color @dropdown_color end |
#input_background ⇒ Object
Returns the value of attribute input_background.
4 5 6 |
# File 'lib/dvl/color/generator.rb', line 4 def input_background @input_background end |
#input_background_focus ⇒ Object
Returns the value of attribute input_background_focus.
4 5 6 |
# File 'lib/dvl/color/generator.rb', line 4 def input_background_focus @input_background_focus end |
#input_color ⇒ Object
Returns the value of attribute input_color.
4 5 6 |
# File 'lib/dvl/color/generator.rb', line 4 def input_color @input_color end |
Instance Method Details
#base_color ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dvl/color/generator.rb', line 84 def base_color @base_color ||= begin text = background_color.dup # If the background is dark, we lighten it. # If the background is light, we darken it. amt = if background_color.brightness < DARK_BACKGROUND_CUTOFF # Dark 1 else -1 end target = target_contrast_ratio while contrast(background_color, text) < target new_text = text.lighten(amt) # prevent infinite loop break if text == new_text text = new_text end # If the background is a conservative color, use a conservative # (desaturated) text color. if conservative_background_dark? || conservative_background_light? hsl = text.hsl hsl.s = hsl.s * 0.15 text = Chroma.paint(hsl) end text end end |
#black ⇒ Object
195 196 197 |
# File 'lib/dvl/color/generator.rb', line 195 def black Chroma.paint('#000') end |
#conservative_background_dark? ⇒ Boolean
66 67 68 |
# File 'lib/dvl/color/generator.rb', line 66 def conservative_background_dark? background_color.brightness < 52 end |
#conservative_background_light? ⇒ Boolean
70 71 72 |
# File 'lib/dvl/color/generator.rb', line 70 def conservative_background_light? background_color.brightness > 229 end |
#error_bubble_background ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/dvl/color/generator.rb', line 171 def error_bubble_background if error_color.brightness > 200 white else error_color end end |
#error_bubble_color ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/dvl/color/generator.rb', line 179 def error_bubble_color if error_bubble_background == white background_color else white end end |
#error_color ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/dvl/color/generator.rb', line 120 def error_color @error_color ||= begin error = background_color.dup # Spin the color wheel until we land on "red" amt = if error.hsl.h > 180 15 else -15 end while !red?(error) error = error.spin(amt) end # White if background_color == white error = Chroma.paint('d95b76') # Black elsif background_color == black error = Chroma.paint('d95b76') # Grayscale elsif error.hsl.s == 0 error = error.saturate(100) # Otherwise, ensure saturation is at least 0.4, or else it's not really red... elsif error.hsl.s < 0.4 hsl = error.hsl hsl.s = 0.4 error = Chroma.paint(hsl) elsif conservative_background_light? hsl = error.hsl hsl.l = hsl.l - 0.4 error = Chroma.paint(hsl) end amt = if background_color.brightness < 100 2 else -2 end while contrast(error, background_color) < 2.5 new_error = error.lighten(amt) break if error == new_error error = new_error end error end end |
#generate_dropdown_colors ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/dvl/color/generator.rb', line 187 def generate_dropdown_colors if background_color.brightness < DARK_BACKGROUND_CUTOFF [base_color, background_color] else [background_color, base_color] end end |
#target_contrast_ratio ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/dvl/color/generator.rb', line 74 def target_contrast_ratio if conservative_background_dark? 13 elsif conservative_background_light? 10 else 7 end end |
#to_debug_s ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dvl/color/generator.rb', line 46 def to_debug_s str = [] to_h.each do |k, v| str << "#{k}: #{v}" end str << "Background brightness: #{background_color.brightness}" str << "Ratio: #{contrast(background_color, base_color).round(2)}" str << "Error Ratio: #{contrast(background_color, error_color).round(2)}" str << "Error Bubble Ratio: #{contrast(error_bubble_background, error_bubble_color).round(2)}" str << "Input ratio: #{contrast(input_background, input_color).round(2)}" str << "Error satur: #{error_color.hsl.s.round(2)}" str << "Conservative light: #{conservative_background_light?}" str << "Conservative dark: #{conservative_background_dark?}" str.join("<br>") end |
#to_h ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dvl/color/generator.rb', line 23 def to_h { backgroundColor: background_color.to_hex, baseColor: base_color.to_hex, errorColor: error_color.to_hex, errorBubbleBackground: error_bubble_background.to_hex, errorBubbleColor: error_bubble_color.to_hex, inputBackground: input_background.to_hex, inputBackgroundFocus: input_background_focus.to_hex, inputColor: input_color.to_hex, dropdownBackground: dropdown_background.to_hex, dropdownColor: dropdown_color.to_hex } end |
#to_scss ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/dvl/color/generator.rb', line 38 def to_scss ''.tap do |str| to_h.each do |k, v| str << "$#{k}:#{v};" end end end |
#white ⇒ Object
199 200 201 |
# File 'lib/dvl/color/generator.rb', line 199 def white Chroma.paint('#fff') end |