@@ -12,6 +12,7 @@ import std.math;
12
12
import std.regex ;
13
13
import std.stdio ;
14
14
import std.string ;
15
+ import std.traits ;
15
16
import std.uni : toLower;
16
17
import std.traits : isNumeric;
17
18
@@ -21,9 +22,13 @@ import sily.array;
21
22
22
23
// / GLSL style alias to Color
23
24
alias col = Color;
25
+ alias Color8 = color8;
24
26
25
27
// / Constructs color from 8 bit (0-255) components
26
- alias Color8 = (float R, float G, float B, float A = 255 ) => Color(R / 255.0f , G / 255.0f , B / 255.0f , A / 255.0f );
28
+ private Color color8 (float R, float G, float B, float A = 255 ) {
29
+ import sily.color;
30
+ return Color (R / 255.0f , G / 255.0f , B / 255.0f , A / 255.0f );
31
+ }
27
32
28
33
// / Color structure with data accesible with `[N]` or swizzling
29
34
struct Color {
@@ -131,7 +136,10 @@ struct Color {
131
136
bool opEquals (R)(in Color! (R, 4 ) b) const if ( isNumeric! R ) {
132
137
// assert(/* this !is null && */ b !is null, "\nOP::ERROR nullptr Color!" ~ 4.to!string ~ ".");
133
138
bool eq = true ;
134
- foreach (i; 0 .. 4 ) { eq = eq && data[i] == b.data[i]; }
139
+ foreach (i; 0 .. 4 ) {
140
+ eq = eq && data[i] == b.data[i];
141
+ if (! eq) break ;
142
+ }
135
143
return eq;
136
144
}
137
145
@@ -168,6 +176,32 @@ struct Color {
168
176
return this ;
169
177
}
170
178
179
+ // / opCast cast(x) y
180
+ R opCast (R)() const if (isVector! R && (R.size == 3 || R.size == 4 ) && isFloatingPoint! (R.dataType)){
181
+ R ret;
182
+ foreach (i; 0 .. R.size) {
183
+ ret[i] = cast (R.dataType) data[i];
184
+ }
185
+ return ret;
186
+ }
187
+ // / Ditto
188
+ R opCast (R)() const if (isVector! R && (R.size == 3 || R.size == 4 ) && ! isFloatingPoint! (R.dataType)){
189
+ R ret;
190
+ foreach (i; 0 .. R.size) {
191
+ ret[i] = cast (R.dataType) (data[i] * 255.0f );
192
+ }
193
+ return ret;
194
+ }
195
+ // / Ditto
196
+ bool opCast (T)() const if (is (T == bool )) {
197
+ float s = 0 ;
198
+ foreach (i; 0 .. 4 ) {
199
+ s += data[i];
200
+ }
201
+ return ! s.isClose(0 , float .epsilon);
202
+ }
203
+
204
+
171
205
// / Returns hash
172
206
size_t toHash () const @safe nothrow {
173
207
return typeid (data).getHash(&data);
@@ -179,23 +213,23 @@ struct Color {
179
213
enum AccessString = " r g b a" ; // exclude from docs
180
214
mixin accessByString! (float , 4 , " data" , AccessString); // exclude from docs
181
215
182
- /**
183
- Returns color transformed to float vector.
184
- Also direct assign syntax is allowed:
185
- ---
186
- // Assigns rgba values
187
- Vector!(float, 4) v4 = Color(0.4, 1.0);
188
- // Only rgb values
189
- Vector!(float, 3) v3 = Color(0.7);
190
- ---
191
- */
192
- public Vector4f asVector4f () {
193
- return Vector4f (data);
194
- }
195
- // / Ditto
196
- public Vector3f asVector3f () {
197
- return Vector3f (data[0 .. 3 ]);
198
- }
216
+ // / **
217
+ // Returns color transformed to float vector.
218
+ // Also direct assign syntax is allowed:
219
+ // ---
220
+ // // Assigns rgba values
221
+ // Vector!(float, 4) v4 = Color(0.4, 1.0);
222
+ // // Only rgb values
223
+ // Vector!(float, 3) v3 = Color(0.7);
224
+ // ---
225
+ // */
226
+ // public vec4 asVector4f() {
227
+ // return vec4 (data);
228
+ // }
229
+ // // / Ditto
230
+ // public vec3 asVector3f() {
231
+ // return vec3 (data[0..3]);
232
+ // }
199
233
200
234
// / Returns copy of color
201
235
public Color copyof () {
@@ -891,4 +925,4 @@ enum Colors: Color {
891
925
whiteSmoke = Color8(245 ,245 ,245 ), // / <font color=whiteSmoke>◼</font>
892
926
yellow = Color8(255 ,255 ,0 ), // / <font color=yellow>◼</font>
893
927
yellowGreen = Color8(154 ,205 ,50 ) // / <font color=yellowGreen>◼</font>
894
- }
928
+ }
0 commit comments