Class: RainbowColors::Convertor

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

Class Method Summary collapse

Class Method Details

.hex_from_hsl(color_hsl) ⇒ Object



90
91
92
93
# File 'lib/rainbow_colors/convertor.rb', line 90

def self.hex_from_hsl(color_hsl)
  rgb = rgb_from_hsl color_hsl
  hex = hex_from_rgb rgb
end

.hex_from_rgb(color_rgb) ⇒ Object

color_rgb => { red: 100, green: 100, blue: 100 }



14
15
16
17
18
19
20
# File 'lib/rainbow_colors/convertor.rb', line 14

def self.hex_from_rgb(color_rgb)
  red   = color_rgb[:red]   < 16 ? "0" + color_rgb[:red].to_s(16)   : color_rgb[:red].to_s(16)
  green = color_rgb[:green] < 16 ? "0" + color_rgb[:green].to_s(16) : color_rgb[:green].to_s(16)
  blue  = color_rgb[:blue]  < 16 ? "0" + color_rgb[:blue].to_s(16)  : color_rgb[:blue].to_s(16)
  
  ("##{red}#{green}#{blue}").upcase
end

.hsl_from_hex(color_hex) ⇒ Object



85
86
87
88
# File 'lib/rainbow_colors/convertor.rb', line 85

def self.hsl_from_hex(color_hex)
  rgb = rgb_from_hex color_hex
  hsl = hsl_from_rgb rgb
end

.hsl_from_rgb(color_rgb) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rainbow_colors/convertor.rb', line 22

def self.hsl_from_rgb(color_rgb)
  red   = color_rgb[:red]   / 255.0
  green = color_rgb[:green] / 255.0
  blue  = color_rgb[:blue]  / 255.0
  
  min = [red, green, blue].min
  max = [red, green, blue].max
  delta_rgb = max - min
  
  luminance = (min + max) / 2
  
  if delta_rgb == 0
    hue = 0
    saturation = 0
  else
    if luminance < 0.5
      saturation = delta_rgb / (max + min)
    else
      saturation = delta_rgb / (2.0 - max - min)
    end
  
    delta_red   = (((max - red)   / 6.0) + (delta_rgb / 2.0)) / delta_rgb
    delta_green = (((max - green) / 6.0) + (delta_rgb / 2.0)) / delta_rgb
    delta_blue  = (((max - blue)  / 6.0) + (delta_rgb / 2.0)) / delta_rgb
    
    hue = case max
          when red
            delta_blue - delta_green
          when green
            1.0/3.0 + delta_red - delta_blue
          when blue
            2.0/3.0 + delta_green - delta_red
          end
  end
  
  hue = hue + 1 if hue < 0
  hue = hue - 1 if hue > 1
  
  { hue: hue * 360, saturation: saturation * 100, luminance: luminance * 100 }
end

.rgb_from_hex(color_hex) ⇒ Object

color_hex => #AB10CD



5
6
7
8
9
10
11
# File 'lib/rainbow_colors/convertor.rb', line 5

def self.rgb_from_hex(color_hex)
  red   = (color_hex[1] + color_hex[2]).to_i(16).round
  green = (color_hex[3] + color_hex[4]).to_i(16).round
  blue  = (color_hex[5] + color_hex[6]).to_i(16).round
  
  { red: red, green: green, blue: blue }
end

.rgb_from_hsl(color_hsl) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rainbow_colors/convertor.rb', line 63

def self.rgb_from_hsl(color_hsl)
  hue = color_hsl[:hue] / 360
  saturation = color_hsl[:saturation] / 100
  luminance  = color_hsl[:luminance]  / 100
  
  red = blue = green = luminance * 255
  
  if saturation != 0
    if luminance < 0.5
      v2 = luminance * (1.0 + saturation)
    else
      v2 = (luminance + saturation) - (luminance * saturation)
    end
    v1 = 2.0 * luminance - v2
    red   = (255 * rgb_from_hue(v1, v2, 1.0/3.0 + hue)).round
    green = (255 * rgb_from_hue(v1, v2, hue)).round
    blue  = (255 * rgb_from_hue(v1, v2, hue - 1.0/3.0)).round
  end
  
  { red: red.round, green: green.round, blue: blue.round }
end