76fbfc6f7a1c11e933c85e4d051ac3ac3ee3ba85
[GPU.git] / ColorTools_GPU.h
1 #ifndef COLOR_TOOL_GPU_H
2 #define COLOR_TOOL_GPU_H
3
4 #include "both_define.h"
5 #include "builtin_types.h" // pour float3
6
7
8 // TODO cuda ameliorer
9
10 namespace gpu
11 {
12
13 class ColorTools
14 {
15 /*--------------------------------------*\
16 |* HSB_TO_RVB *|
17 \*-------------------------------------*/
18
19 /*--------------------------------------*\
20 |* public *|
21 \*-------------------------------------*/
22
23 public:
24
25 /**
26 * Convertir une couleur HSB en RGB !
27 * H,S,B in [0,1]
28 * R,G,B in [0,255]
29 */
30 __BOTH__
31 static void HSB_TO_RVB(const float3& hsb01, uchar4* ptrRVBA)
32 {
33 ColorTools::HSB_TO_RVB(hsb01.x, hsb01.y, hsb01.z, &ptrRVBA->x, &ptrRVBA->y, &ptrRVBA->z);
34 }
35
36 /**
37 * Convertir une couleur HSB en RGB !
38 * H,S,B in [0,1]
39 * R,G,B in [0,255]
40 */
41 __BOTH__
42 static void HSB_TO_RVB(float h01, uchar4* ptrRVBA)
43 {
44 ColorTools::HSB_TO_RVB(h01, 1.0f, 1.0f, &ptrRVBA->x, &ptrRVBA->y, &ptrRVBA->z);
45 }
46
47 /**
48 * Convertir une couleur HSB en RGB !
49 * H,S,B in [0,1]
50 * R,G,B in [0,255]
51 */
52 __BOTH__
53 static void HSB_TO_RVB(float h01, float s01, float b01, uchar4* ptrRVBA)
54 {
55 ColorTools::HSB_TO_RVB(h01, s01, b01, &ptrRVBA->x, &ptrRVBA->y, &ptrRVBA->z);
56 }
57
58 /**
59 * Convertir une couleur HSB en RGB !
60 * H,S,B in [0,1]
61 * R,G,B in [0,255]
62 */
63 __BOTH__
64 static void HSB_TO_RVB(float H, float S, float V, unsigned char *ptrR, unsigned char *ptrG, unsigned char *ptrB)
65 {
66 //float H = profondeur / 255.0;
67 //float S = 1;
68 //float V = 1;
69 if (S == 0) //HSV from 0 to 1
70 {
71 *ptrR = V * 255;
72 *ptrG = V * 255;
73 *ptrB = V * 255;
74 }
75 else
76 {
77 float var_h = H * 6;
78 if (var_h == 6)
79 {
80 var_h = 0;
81 } //H must be < 1
82
83 unsigned char var_i = (unsigned char) var_h; //Or ... var_i = floor( var_h )
84
85 float var_1 = V * (1 - S);
86 float var_2 = V * (1 - S * (var_h - var_i));
87 float var_3 = V * (1 - S * (1 - (var_h - var_i)));
88
89 float var_r, var_g, var_b;
90 if (var_i == 0)
91 {
92 var_r = V;
93 var_g = var_3;
94 var_b = var_1;
95 }
96 else if (var_i == 1)
97 {
98 var_r = var_2;
99 var_g = V;
100 var_b = var_1;
101 }
102 else if (var_i == 2)
103 {
104 var_r = var_1;
105 var_g = V;
106 var_b = var_3;
107 }
108 else if (var_i == 3)
109 {
110 var_r = var_1;
111 var_g = var_2;
112 var_b = V;
113 }
114 else if (var_i == 4)
115 {
116 var_r = var_3;
117 var_g = var_1;
118 var_b = V;
119 }
120 else
121 {
122 var_r = V;
123 var_g = var_1;
124 var_b = var_2;
125 }
126
127 //RGB results from 0 to 255
128 *ptrR = (unsigned char) (var_r * 255);
129 *ptrG = (unsigned char) (var_g * 255);
130 *ptrB = (unsigned char) (var_b * 255);
131 }
132 }
133
134 /*--------------------------------------*\
135 |* private *|
136 \*-------------------------------------*/
137
138 };
139 }
140 #endif
141
142 /*----------------------------------------------------------------------*\
143 |* End *|
144 \*---------------------------------------------------------------------*/