Skip to content

Commit 8d5cd19

Browse files
author
al1-ce
committed
Added matrix and quaterions
1 parent bb64b79 commit 8d5cd19

File tree

8 files changed

+2189
-413
lines changed

8 files changed

+2189
-413
lines changed

Diff for: core/sily/color.d

+54-20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import std.math;
1212
import std.regex;
1313
import std.stdio;
1414
import std.string;
15+
import std.traits;
1516
import std.uni : toLower;
1617
import std.traits: isNumeric;
1718

@@ -21,9 +22,13 @@ import sily.array;
2122

2223
/// GLSL style alias to Color
2324
alias col = Color;
25+
alias Color8 = color8;
2426

2527
/// 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+
}
2732

2833
/// Color structure with data accesible with `[N]` or swizzling
2934
struct Color {
@@ -131,7 +136,10 @@ struct Color {
131136
bool opEquals(R)(in Color!(R, 4) b) const if ( isNumeric!R ) {
132137
// assert(/* this !is null && */ b !is null, "\nOP::ERROR nullptr Color!" ~ 4.to!string ~ ".");
133138
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+
}
135143
return eq;
136144
}
137145

@@ -168,6 +176,32 @@ struct Color {
168176
return this;
169177
}
170178

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+
171205
/// Returns hash
172206
size_t toHash() const @safe nothrow {
173207
return typeid(data).getHash(&data);
@@ -179,23 +213,23 @@ struct Color {
179213
enum AccessString = "r g b a"; // exclude from docs
180214
mixin accessByString!(float, 4, "data", AccessString); // exclude from docs
181215

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+
// }
199233

200234
/// Returns copy of color
201235
public Color copyof() {
@@ -891,4 +925,4 @@ enum Colors: Color {
891925
whiteSmoke = Color8(245,245,245), /// <font color=whiteSmoke>&#x25FC;</font>
892926
yellow = Color8(255,255,0), /// <font color=yellow>&#x25FC;</font>
893927
yellowGreen = Color8(154,205,50) /// <font color=yellowGreen>&#x25FC;</font>
894-
}
928+
}

Diff for: core/sily/math.d

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ T snap(T)(T p_val, T p_step) if (isFloatingPoint!T) {
3636
return p_val;
3737
}
3838

39+
/// Snaps value to step, if T is not float, then explicit casts are used
40+
/// and it may cause data loss
41+
T snap(T)(T p_val, T p_step) if (!isFloatingPoint!T) {
42+
if (p_step != 0) {
43+
p_val = cast(T) (floor(cast(double) p_val / cast(double) p_step + 0.5) * cast(double) p_step);
44+
}
45+
return p_val;
46+
}
47+
48+
3949
/// std.random wrapper
4050
struct RNG {
4151
private uint _seed = defaultSeed;

0 commit comments

Comments
 (0)