-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgbTOhsv.rb
74 lines (63 loc) · 1.54 KB
/
rgbTOhsv.rb
1
2
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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
def min(v1, v2, v3)
if (v1 <= v2 && v1 <= v3)
return v1;
end
if (v2 <= v1 && v2 <= v3)
return v2;
end
if (v3 <= v1 && v3 <= v2)
return v3;
end
end
def max(v1, v2, v3)
if (v1 >= v2 && v1 >= v3)
return v1;
end
if (v2 >= v1 && v2 >= v3)
return v2;
end
if (v3 >= v1 && v3 >= v2)
return v3;
end
end
r, g, b = ARGV;
r = r.to_f;
g = g.to_f;
b = b.to_f;
h, s, v = 0.0;
min = min(r,g,b);
puts "Minimum of #{r}, #{g} and #{b} = #{min}";
max = max(r,g,b);
puts "Maximum of #{r}, #{g} and #{b} = #{max}";
v = max;
delta = max - min;
puts "V is #{max} and delta is #{delta}";
if (v == 0)
s = 0;
puts("V == 0, therefore S = 0");
else
s = delta/v
puts("V != 0, therefore S = Delta/V = #{s}");
end
if (s == 0)
h = 0;
#abort("Error I guess since V and S are 0?!")
else
if (r == v)
h = 60 * ((g - b)/delta);
puts("R == V, therefore H = 60 * (G - B)/Delta = #{h}")
elsif (g == v)
h = 120 + 60 * ((b - r)/delta)
puts("G == V, therefore H = 120 + 60 * (B - R)/Delta = #{h}")
else
h = 240 + 60 * ((r - g)/delta)
puts("G == V, therefore H = 2400 + 60 * (R - G)/Delta = #{h}")
end
end
if (h < 0)
h = h + 360;
puts ("H is smaller than 0, therefore H = H + 360 = #{h} and V = V / 255 = #{v}")
end
v = v / 255 * 100;
s = s * 100;
puts ("Final result:\nHue is\t#{h}°\nSaturation is\t#{s}%\nValue is\t#{v}%")