Class: RainbowColors::Palette

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

Class Method Summary collapse

Class Method Details

.adjust_saturation(colors, variance) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rainbow_colors/palette.rb', line 11

def self.adjust_saturation(colors, variance)
  colors = colors.map do |c|
    hsl = RainbowColors::Convertor.hsl_from_hex c
    if variance > 0
      hsl[:saturation] = hsl[:saturation] + variance if hsl[:saturation] + variance <= 100
    else
      hsl[:saturation] = hsl[:saturation] + variance if hsl[:saturation] + variance >= 0
    end
    RainbowColors::Convertor.hex_from_hsl hsl
  end
end

.color_accent(color_scheme, color_background) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rainbow_colors/palette.rb', line 36

def self.color_accent(color_scheme, color_background)
  bg = RainbowColors::Convertor.rgb_from_hex color_background
  color_scheme = color_scheme.map { |hex| RainbowColors::Convertor.rgb_from_hex(hex) }
  
  contrast_colors = []
  color_scheme.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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rainbow_colors/palette.rb', line 23

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

.random_colorObject



3
4
5
6
7
8
9
# File 'lib/rainbow_colors/palette.rb', line 3

def self.random_color
  red   = rand(255)
  green = rand(255)
  blue  = rand(255)
  
  RainbowColors::Convertor.hex_from_rgb({ red: red, green: green, blue: blue })
end