-
Notifications
You must be signed in to change notification settings - Fork 5
/
color.go
100 lines (92 loc) · 2.46 KB
/
color.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package imgui
func ColorConvertFloat4ToU32(in ImVec4) ImU32 {
var out ImU32
out = ((ImU32)(IM_F32_TO_INT8_SAT(in.x))) << IM_COL32_R_SHIFT
out |= ((ImU32)(IM_F32_TO_INT8_SAT(in.y))) << IM_COL32_G_SHIFT
out |= ((ImU32)(IM_F32_TO_INT8_SAT(in.z))) << IM_COL32_B_SHIFT
out |= ((ImU32)(IM_F32_TO_INT8_SAT(in.w))) << IM_COL32_A_SHIFT
return out
}
func ImAlphaBlendColors(col_a, col_b ImU32) ImU32 {
var t = float((col_b>>IM_COL32_A_SHIFT)&0xFF) / 255
var r = int(ImLerp(float((int)(col_a>>IM_COL32_R_SHIFT)&0xFF), float((int)(col_b>>IM_COL32_R_SHIFT)&0xFF), t))
var g = int(ImLerp(float((int)(col_a>>IM_COL32_G_SHIFT)&0xFF), float((int)(col_b>>IM_COL32_G_SHIFT)&0xFF), t))
var b = int(ImLerp(float((int)(col_a>>IM_COL32_B_SHIFT)&0xFF), float((int)(col_b>>IM_COL32_B_SHIFT)&0xFF), t))
return IM_COL32(byte(r), byte(g), byte(b), 0xFF)
}
// ColorConvertU32ToFloat4 Color Utilities
func ColorConvertU32ToFloat4(in ImU32) ImVec4 {
var s float = 1.0 / 255.0
return ImVec4{
float((in>>IM_COL32_R_SHIFT)&0xFF) * s,
float((in>>IM_COL32_G_SHIFT)&0xFF) * s,
float((in>>IM_COL32_B_SHIFT)&0xFF) * s,
float((in>>IM_COL32_A_SHIFT)&0xFF) * s}
}
// ColorConvertRGBtoHSV Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592
// Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
func ColorConvertRGBtoHSV(r float, g float, b float, out_h, out_s, out_v *float) {
var K float = 0
if g < b {
g, b = b, g
K = -1
}
if r < g {
r, g = g, r
K = float(-2)/float(6) - K
}
var chroma = r
if g < b {
chroma -= g
} else {
chroma -= b
}
*out_h = ImFabs(K + (g-b)/(6*chroma+1e-20))
*out_s = chroma / (r + 1e-20)
*out_v = r
}
// ColorConvertHSVtoRGB Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593
// also http://en.wikipedia.org/wiki/HSL_and_HSV
func ColorConvertHSVtoRGB(h float, s float, v float, out_r, out_g, out_b *float) {
if s == 0.0 {
// gray
*out_r = v
*out_g = v
*out_b = v
return
}
h = ImFmod(h, 1.0) / (60.0 / 360.0)
var i = (int)(h)
var f = h - (float)(i)
var p = v * (1.0 - s)
var q = v * (1.0 - s*f)
var t = v * (1.0 - s*(1.0-f))
switch i {
case 0:
*out_r = v
*out_g = t
*out_b = p
case 1:
*out_r = q
*out_g = v
*out_b = p
case 2:
*out_r = p
*out_g = v
*out_b = t
case 3:
*out_r = p
*out_g = q
*out_b = v
case 4:
*out_r = t
*out_g = p
*out_b = v
case 5:
fallthrough
default:
*out_r = v
*out_g = p
*out_b = q
}
}