Module: ColorSpaceConverter::Compute

Included in:
ColorSpace
Defined in:
lib/color_space_converter/compute.rb

Class Method Summary collapse

Class Method Details

.hex2rgb(hex) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/color_space_converter/compute.rb', line 13

def hex2rgb(hex)
  r = hex[1..2]
  g = hex[3..4]
  b = hex[5..6]
  rgb = []
  [r, g, b].each do |s|
    rgb << s.hex
  end
  rgb
end

.hsv2rgb(h, s, v) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/color_space_converter/compute.rb', line 152

def hsv2rgb(h, s, v)
  rgb = []
  h = 0 if h == 360
  s = s.to_f / 100
  v = v.to_f / 100

  if s.zero?
    r = v * 255
    g = v * 255
    b = v * 255
    return [r.to_i, g.to_i, b.to_i]
  end

  dh = h.to_f / 60
  p = v * (1 - s)
  q = v * (1 - s * (h.to_f / 60 - dh))
  t = v * (1 - s * (1 - (h.to_f / 60 - dh)))

  # rubocop: disable Style/Semicolon
  case dh
  when 0
    r = v; g = t; b = p;
  when 1
    r = q; g = v; b = p;
  when 2
    r = p; g = v; b = t;
  when 3
    r = p; g = q; b = v;
  when 4
    r = t; g = p; b = v;
  when 5
    r = v; g = p; b = q;
  end
  # rubocop: enable Style/Semicolon

  [r, g, b].each do |n|
    rgb << (n * 255).round
  end
  rgb
end

.lab2rgb(l, a, b, x_n: 100.0, y_n: 100.0, z_n: 100.0) ⇒ Object



122
123
124
125
# File 'lib/color_space_converter/compute.rb', line 122

def lab2rgb(l, a, b, x_n: 100.0, y_n: 100.0, z_n: 100.0)
  x, y, z = lab2xyz(l, a, b, x_n: x_n, y_n: y_n, z_n: z_n)
  xyz2rgb(x, y, z)
end

.lab2xyz(l, a, b, x_n: 100.0, y_n: 100.0, z_n: 100.0) ⇒ Object



80
81
82
83
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
# File 'lib/color_space_converter/compute.rb', line 80

def lab2xyz(l, a, b, x_n: 100.0, y_n: 100.0, z_n: 100.0)
  xyz_n = [x_n, y_n, z_n]
  xyz = []

  if l > 903.3*0.008856
    y = ((l+16)/116)**3
  else
    y = l/903.3
  end

  if y > 0.008856
    fy = (l+16)/116
  else
    fy = (903.3*y+16)/116
  end

  fx =  a/500 + fy
  fz = fy - b/200

  if fz**3 > 0.008856
    x = fx**3
  else
    x = (116*fx-16)/903.3
  end

  if fz**3 > 0.008856
    z = fz**3
  else
    z = (116*fz-16)/903.3
  end

  [x, y, z].each_with_index do |n, i|
    xyz << xyz_n[i] * n
  end
  xyz
end

.rgb2hex(r, g, b) ⇒ Object



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

def rgb2hex(r, g, b)
  hex = '#'
  [r, g, b].each do |n|
    hex += n.to_s(16).rjust(2, '0')
  end
  hex
end

.rgb2hsv(r, g, b) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/color_space_converter/compute.rb', line 127

def rgb2hsv(r, g, b)
  max = [r, g, b].max
  min = [r, g, b].min
  h = 0.0
  s = 0.0
  v = max.to_f

  if max != min
    if max == r
      h = (b-g).to_f/(max-min) * 60
    elsif max == g
      h = (2 + (r-b).to_f/(max-min)) * 60
    elsif max == b
      h = (4 + (g-r).to_f/(max-min)) * 60
    end
    s = (max - min).to_f/ max
  end

  h += 360 if h < 0
  h = h.round
  s = (s*100).round
  v = ((v/255)*100).round
  [h, s, v]
end

.rgb2lab(r, g, b, x_n: 100.0, y_n: 100.0, z_n: 100.0) ⇒ Object



117
118
119
120
# File 'lib/color_space_converter/compute.rb', line 117

def rgb2lab(r, g, b, x_n: 100.0, y_n: 100.0, z_n: 100.0)
  x, y, z = rgb2xyz(r, g, b)
  xyz2lab(x, y, z, x_n: x_n, y_n: y_n, z_n: z_n)
end

.rgb2xyz(r, g, b) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/color_space_converter/compute.rb', line 24

def rgb2xyz(r, g, b)
  s_rgb = []
  [r, g, b].each do |n|
    n = n.to_f / 255
    if n <= 0.0405
      s_n = n / 12.92
    else
      s_n = ((n+0.055) / 1.055) ** 2.4
    end
    s_rgb << s_n
  end

  x = s_rgb[0]*0.4124 + s_rgb[1]*0.3576 + s_rgb[2]*0.1805
  y = s_rgb[0]*0.2126 + s_rgb[1]*0.7152 + s_rgb[2]*0.0722
  z = s_rgb[0]*0.0193+ s_rgb[1]*0.1192+ s_rgb[2]*0.9505
  [x*100, y*100, z*100]
end

.xyz2lab(x, y, z, x_n: 100.0, y_n: 100.0, z_n: 100.0) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/color_space_converter/compute.rb', line 63

def xyz2lab(x, y, z, x_n: 100.0, y_n: 100.0, z_n: 100.0)
  xyz_n = [x_n, y_n, z_n]
  f_xyz = []
  [x, y, z].each_with_index do |n, i|
    if n/xyz_n[i] < 0.008856
      f = (903.3*n/xyz_n[i]+16)/116
    else
      f = (n/xyz_n[i])**(1.0/3.0)
    end
    f_xyz << f
  end
  l = 116*f_xyz[1] - 16
  a = 500*(f_xyz[0]-f_xyz[1])
  b = 200*(f_xyz[1]-f_xyz[2])
  [l, a, b]
end

.xyz2rgb(x, y, z) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/color_space_converter/compute.rb', line 42

def xyz2rgb(x, y, z)
  x = x / 100
  y = y / 100
  z = z / 100

  s_r = 3.2406*x-1.5372*y-0.4986*z
  s_g = -0.9689*x+1.8758*y+0.0415*z
  s_b = 0.0557*x-0.2040*y+1.0570*z

  rgb = []
  [s_r, s_g, s_b].each do |s_n|
    if s_n <= 0.0031308
      n = s_n * 12.92
    else
      n = 1.055 * (s_n**(1.0/2.4)) - 0.055
    end
    rgb << (n*255).to_i
  end
  rgb
end