Module: Studio::ColorScale
- Defined in:
- lib/studio/color_scale.rb
Constant Summary collapse
- LIGHT_RATIOS =
Mix ratios: how much white (lighter) or black (darker) to mix
{ 50 => 0.95, 100 => 0.85, 200 => 0.70, 300 => 0.50, 400 => 0.30 }.freeze
- DARK_RATIOS =
{ 600 => 0.15, 700 => 0.30, 800 => 0.45, 900 => 0.60 }.freeze
Class Method Summary collapse
- .darken(hex, amount) ⇒ Object
-
.generate(hex) ⇒ Object
Generate a full 50-900 shade scale from a base hex color.
- .hex_to_rgb(hex) ⇒ Object
- .lighten(hex, amount) ⇒ Object
- .rgb_to_hex(r, g, b) ⇒ Object
- .with_opacity(hex, opacity) ⇒ Object
Class Method Details
.darken(hex, amount) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/studio/color_scale.rb', line 53 def self.darken(hex, amount) r, g, b = hex_to_rgb(hex) rgb_to_hex( (r * (1 - amount)).round, (g * (1 - amount)).round, (b * (1 - amount)).round ) end |
.generate(hex) ⇒ Object
Generate a full 50-900 shade scale from a base hex color. 500 = base color, lighter shades mix toward white, darker toward black.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/studio/color_scale.rb', line 21 def self.generate(hex) r, g, b = hex_to_rgb(hex) scale = { 500 => hex.upcase } LIGHT_RATIOS.each do |shade, ratio| scale[shade] = rgb_to_hex( (r + (255 - r) * ratio).round, (g + (255 - g) * ratio).round, (b + (255 - b) * ratio).round ) end DARK_RATIOS.each do |shade, ratio| scale[shade] = rgb_to_hex( (r * (1 - ratio)).round, (g * (1 - ratio)).round, (b * (1 - ratio)).round ) end scale.sort.to_h end |
.hex_to_rgb(hex) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/studio/color_scale.rb', line 62 def self.hex_to_rgb(hex) hex = hex.delete("#") [ hex[0..1].to_i(16), hex[2..3].to_i(16), hex[4..5].to_i(16) ] end |
.lighten(hex, amount) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/studio/color_scale.rb', line 44 def self.lighten(hex, amount) r, g, b = hex_to_rgb(hex) rgb_to_hex( (r + (255 - r) * amount).round, (g + (255 - g) * amount).round, (b + (255 - b) * amount).round ) end |
.rgb_to_hex(r, g, b) ⇒ Object
71 72 73 |
# File 'lib/studio/color_scale.rb', line 71 def self.rgb_to_hex(r, g, b) "#%02X%02X%02X" % [r.clamp(0, 255), g.clamp(0, 255), b.clamp(0, 255)] end |
.with_opacity(hex, opacity) ⇒ Object
75 76 77 78 |
# File 'lib/studio/color_scale.rb', line 75 def self.with_opacity(hex, opacity) r, g, b = hex_to_rgb(hex) "rgba(#{r},#{g},#{b},#{opacity})" end |