Skip to content

Commit

Permalink
Added matrix and quaterions
Browse files Browse the repository at this point in the history
  • Loading branch information
al1-ce committed May 2, 2023
1 parent bb64b79 commit 8d5cd19
Show file tree
Hide file tree
Showing 8 changed files with 2,189 additions and 413 deletions.
74 changes: 54 additions & 20 deletions core/sily/color.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import std.math;
import std.regex;
import std.stdio;
import std.string;
import std.traits;
import std.uni : toLower;
import std.traits: isNumeric;

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

/// GLSL style alias to Color
alias col = Color;
alias Color8 = color8;

/// Constructs color from 8 bit (0-255) components
alias Color8 = (float R, float G, float B, float A = 255) => Color(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
private Color color8(float R, float G, float B, float A = 255) {
import sily.color;
return Color(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
}

/// Color structure with data accesible with `[N]` or swizzling
struct Color {
Expand Down Expand Up @@ -131,7 +136,10 @@ struct Color {
bool opEquals(R)(in Color!(R, 4) b) const if ( isNumeric!R ) {
// assert(/* this !is null && */ b !is null, "\nOP::ERROR nullptr Color!" ~ 4.to!string ~ ".");
bool eq = true;
foreach (i; 0 .. 4) { eq = eq && data[i] == b.data[i]; }
foreach (i; 0 .. 4) {
eq = eq && data[i] == b.data[i];
if (!eq) break;
}
return eq;
}

Expand Down Expand Up @@ -168,6 +176,32 @@ struct Color {
return this;
}

/// opCast cast(x) y
R opCast(R)() const if (isVector!R && (R.size == 3 || R.size == 4) && isFloatingPoint!(R.dataType)){
R ret;
foreach (i; 0 .. R.size) {
ret[i] = cast(R.dataType) data[i];
}
return ret;
}
/// Ditto
R opCast(R)() const if (isVector!R && (R.size == 3 || R.size == 4) && !isFloatingPoint!(R.dataType)){
R ret;
foreach (i; 0 .. R.size) {
ret[i] = cast(R.dataType) (data[i] * 255.0f);
}
return ret;
}
/// Ditto
bool opCast(T)() const if (is(T == bool)) {
float s = 0;
foreach (i; 0..4) {
s += data[i];
}
return !s.isClose(0, float.epsilon);
}


/// Returns hash
size_t toHash() const @safe nothrow {
return typeid(data).getHash(&data);
Expand All @@ -179,23 +213,23 @@ struct Color {
enum AccessString = "r g b a"; // exclude from docs
mixin accessByString!(float, 4, "data", AccessString); // exclude from docs

/**
Returns color transformed to float vector.
Also direct assign syntax is allowed:
---
// Assigns rgba values
Vector!(float, 4) v4 = Color(0.4, 1.0);
// Only rgb values
Vector!(float, 3) v3 = Color(0.7);
---
*/
public Vector4f asVector4f() {
return Vector4f(data);
}
/// Ditto
public Vector3f asVector3f() {
return Vector3f(data[0..3]);
}
// /**
// Returns color transformed to float vector.
// Also direct assign syntax is allowed:
// ---
// // Assigns rgba values
// Vector!(float, 4) v4 = Color(0.4, 1.0);
// // Only rgb values
// Vector!(float, 3) v3 = Color(0.7);
// ---
// */
// public vec4 asVector4f() {
// return vec4(data);
// }
// /// Ditto
// public vec3 asVector3f() {
// return vec3(data[0..3]);
// }

/// Returns copy of color
public Color copyof() {
Expand Down Expand Up @@ -891,4 +925,4 @@ enum Colors: Color {
whiteSmoke = Color8(245,245,245), /// <font color=whiteSmoke>&#x25FC;</font>
yellow = Color8(255,255,0), /// <font color=yellow>&#x25FC;</font>
yellowGreen = Color8(154,205,50) /// <font color=yellowGreen>&#x25FC;</font>
}
}
10 changes: 10 additions & 0 deletions core/sily/math.d
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ T snap(T)(T p_val, T p_step) if (isFloatingPoint!T) {
return p_val;
}

/// Snaps value to step, if T is not float, then explicit casts are used
/// and it may cause data loss
T snap(T)(T p_val, T p_step) if (!isFloatingPoint!T) {
if (p_step != 0) {
p_val = cast(T) (floor(cast(double) p_val / cast(double) p_step + 0.5) * cast(double) p_step);
}
return p_val;
}


/// std.random wrapper
struct RNG {
private uint _seed = defaultSeed;
Expand Down
Loading

0 comments on commit 8d5cd19

Please sign in to comment.