Class: RainbowColors::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/rainbow_colors/palette.rb

Class Method Summary collapse

Class Method Details

.color_accent(color_palette, color_background) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rainbow_colors/palette.rb', line 43

def self.color_accent(color_palette, color_background)
  bg = RainbowColors::Convertor.rgb_from_hex color_background
  color_palette = color_scheme.map { |hex| RainbowColors::Convertor.rgb_from_hex(hex) }
  
  contrast_colors = []
  color_palette.each do |c|
    delta_red   = [bg[:red],   c[:red]].max   - [bg[:red],   c[:red]].min
    delta_green = [bg[:green], c[:green]].max - [bg[:green], c[:green]].min
    delta_blue  = [bg[:blue],  c[:blue]].max  - [bg[:blue],  c[:blue]].min
    color_difference = delta_red + delta_green + delta_blue
    
    contrast_colors << RainbowColors::Convertor.hex_from_rgb(c) if color_difference > 250
  end
  
  # Return random contrasting color color, if available
  contrast_colors.empty? ? self.color_text(color_background) : contrast_colors.sample
end

.color_text(color_background) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rainbow_colors/palette.rb', line 30

def self.color_text(color_background)
  background_rgb = RainbowColors::Convertor.rgb_from_hex color_background
  
  # http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
  background_brightness = Math.sqrt(
    background_rgb[:red]   ** 2 * 0.241 +
    background_rgb[:green] ** 2 * 0.691 +
    background_rgb[:blue]  ** 2 * 0.068
  )
  
  background_brightness < 145 ? "#E6E6E6" : "#1A1A1A"
end

.palette(base_color, scheme = :complementary) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rainbow_colors/palette.rb', line 3

def self.palette(base_color, scheme = :complementary)
  grays   = RainbowColors::Scheme.harmonious_grays(base_color)
  
  palette = case scheme
            when :analogous
              RainbowColors::Scheme.analogous(base_color)
            when :complementary
              RainbowColors::Scheme.complementary(base_color)
            when :complementary_split
              RainbowColors::Scheme.complementary_split(base_color)
            when :triad
              RainbowColors::Scheme.triad(base_color)
            when :shades
              RainbowColors::Scheme.shades(base_color)
            when :tints
              RainbowColors::Scheme.tints(base_color)
            end
  palette.shift # Remove base color
  
  {
    color_base:   base_color,
    color_shade:  grays.first,
    color_tint:   grays.last,
    color_accent: palette.sample
  }
end