summaryrefslogtreecommitdiff
path: root/src/color.h
diff options
context:
space:
mode:
authornavewindre <nw@moneybot.cc>2024-07-17 08:36:51 +0200
committernavewindre <nw@moneybot.cc>2024-07-17 08:36:51 +0200
commit819cf3e4e8abb5ad41d8830155fc20bd833b21e8 (patch)
tree463951e47fc2937979542c8c46d14b9e0da827ce /src/color.h
parent4c8b52fc94c04c4b3d338c2501971ae348f5b3e5 (diff)
render wip
Diffstat (limited to 'src/color.h')
-rw-r--r--src/color.h425
1 files changed, 425 insertions, 0 deletions
diff --git a/src/color.h b/src/color.h
new file mode 100644
index 0000000..1717023
--- /dev/null
+++ b/src/color.h
@@ -0,0 +1,425 @@
+#pragma once
+#include "typedef.h"
+
+struct FCLR;
+struct ICLR;
+struct CLR;
+
+// float to int conversion
+#pragma warning( disable: 4244 )
+
+struct CLR {
+ U8 r, g, b, a;
+
+ CLR operator*( float v ) {
+ CLR ret = *this;
+
+ ret.r *= v;
+ ret.g *= v;
+ ret.b *= v;
+ ret.a *= v;
+
+ return ret;
+ }
+
+ CLR operator/( float v ) {
+ CLR ret = *this;
+
+ float inv = 1.f / v;
+
+ ret.r *= inv;
+ ret.g *= inv;
+ ret.b *= inv;
+ ret.a *= inv;
+
+ return ret;
+ }
+
+ CLR operator/=( float v ) {
+ float inv = 1.f / v;
+
+ r *= inv;
+ g *= inv;
+ b *= inv;
+ a *= inv;
+
+ return *this;
+ }
+
+ CLR operator*=( float v ) {
+ r *= v;
+ g *= v;
+ b *= v;
+ a *= v;
+
+ return *this;
+ }
+
+ CLR operator+( const CLR& other ) {
+ CLR ret = *this;
+
+ ret.r += other.r;
+ ret.g += other.g;
+ ret.b += other.b;
+ ret.a += other.a;
+
+ return ret;
+ }
+
+ CLR operator-( const CLR& other ) {
+ CLR ret = *this;
+
+ ret.r -= other.r;
+ ret.g -= other.g;
+ ret.b -= other.b;
+ ret.a -= other.a;
+
+ return ret;
+ }
+
+ CLR operator+=( const CLR& other ) {
+ r += other.r;
+ g += other.g;
+ b += other.b;
+ a += other.a;
+
+ return *this;
+ }
+
+ CLR operator-=( const CLR& other ) {
+ r -= other.r;
+ g -= other.g;
+ b -= other.b;
+ a -= other.a;
+
+ return *this;
+ }
+
+ static CLR from_hsb( F32 h, F32 s, F32 b ) {
+ F32 c = s * b;
+ F32 x = c * (1 - abs((int)(h / 60) % 2 - 1));
+ F32 m = b - c;
+
+ F32 _r, _g, _b;
+
+ if (h < 60) {
+ _r = c;
+ _g = x;
+ _b = 0;
+ } else if (h < 120) {
+ _r = x;
+ _g = c;
+ _b = 0;
+ } else if (h < 180) {
+ _r = 0;
+ _g = c;
+ _b = x;
+ } else if (h < 240) {
+ _r = 0;
+ _g = x;
+ _b = c;
+ } else if (h < 300) {
+ _r = x;
+ _g = 0;
+ _b = c;
+ } else {
+ _r = c;
+ _g = 0;
+ _b = x;
+ }
+
+ return {
+ (U8)((_r + m) * 255),
+ (U8)((_g + m) * 255),
+ (U8)((_b + m) * 255),
+ 255
+ };
+ }
+};
+
+struct FCLR {
+ F32 r, g, b, a;
+
+ FCLR operator*( float v ) {
+ FCLR ret = *this;
+
+ ret.r *= v;
+ ret.g *= v;
+ ret.b *= v;
+ ret.a *= v;
+
+ return ret;
+ }
+
+ FCLR operator/( float v ) {
+ FCLR ret = *this;
+
+ float inv = 1.f / v;
+
+ ret.r *= inv;
+ ret.g *= inv;
+ ret.b *= inv;
+ ret.a *= inv;
+
+ return ret;
+ }
+
+ FCLR operator/=( float v ) {
+ float inv = 1.f / v;
+
+ r *= inv;
+ g *= inv;
+ b *= inv;
+ a *= inv;
+
+ return *this;
+ }
+
+ FCLR operator*=( float v ) {
+ r *= v;
+ g *= v;
+ b *= v;
+ a *= v;
+
+ return *this;
+ }
+
+ FCLR operator+( const FCLR& other ) {
+ FCLR ret = *this;
+
+ ret.r += other.r;
+ ret.g += other.g;
+ ret.b += other.b;
+ ret.a += other.a;
+
+ return ret;
+ }
+
+ FCLR operator-( const FCLR& other ) {
+ FCLR ret = *this;
+
+ ret.r -= other.r;
+ ret.g -= other.g;
+ ret.b -= other.b;
+ ret.a -= other.a;
+
+ return ret;
+ }
+
+ FCLR operator+=( const FCLR& other ) {
+ r += other.r;
+ g += other.g;
+ b += other.b;
+ a += other.a;
+
+ return *this;
+ }
+
+ FCLR operator-=( const FCLR& other ) {
+ r -= other.r;
+ g -= other.g;
+ b -= other.b;
+ a -= other.a;
+
+ return *this;
+ }
+
+ static FCLR from_hsb( F32 h, F32 s, F32 b ) {
+ F32 c = s * b;
+ F32 x = c * (1 - abs((int)(h / 60) % 2 - 1));
+ F32 m = b - c;
+
+ F32 _r, _g, _b;
+
+ if (h < 60) {
+ _r = c;
+ _g = x;
+ _b = 0;
+ } else if (h < 120) {
+ _r = x;
+ _g = c;
+ _b = 0;
+ } else if (h < 180) {
+ _r = 0;
+ _g = c;
+ _b = x;
+ } else if (h < 240) {
+ _r = 0;
+ _g = x;
+ _b = c;
+ } else if (h < 300) {
+ _r = x;
+ _g = 0;
+ _b = c;
+ } else {
+ _r = c;
+ _g = 0;
+ _b = x;
+ }
+
+ return {
+ _r + m,
+ _g + m,
+ _b + m,
+ 1.f
+ };
+ }
+
+ CLR to_byte() {
+ return {
+ (U8)(r * 255.f),
+ (U8)(g * 255.f),
+ (U8)(b * 255.f),
+ (U8)(a * 255.f)
+ };
+ }
+};
+
+struct ICLR {
+ I32 r, g, b, a;
+
+ ICLR operator*( float v ) {
+ ICLR ret = *this;
+
+ ret.r *= v;
+ ret.g *= v;
+ ret.b *= v;
+ ret.a *= v;
+
+ return ret;
+ }
+
+ ICLR operator/( float v ) {
+ ICLR ret = *this;
+
+ float inv = 1.f / v;
+
+ ret.r *= inv;
+ ret.g *= inv;
+ ret.b *= inv;
+ ret.a *= inv;
+
+ return ret;
+ }
+
+ ICLR operator/=( float v ) {
+ float inv = 1.f / v;
+
+ r *= inv;
+ g *= inv;
+ b *= inv;
+ a *= inv;
+
+ return *this;
+ }
+
+ ICLR operator*=( float v ) {
+ r *= v;
+ g *= v;
+ b *= v;
+ a *= v;
+
+ return *this;
+ }
+
+ ICLR operator+( const ICLR& other ) {
+ ICLR ret = *this;
+
+ ret.r += other.r;
+ ret.g += other.g;
+ ret.b += other.b;
+ ret.a += other.a;
+
+ return ret;
+ }
+
+ ICLR operator-( const ICLR& other ) {
+ ICLR ret = *this;
+
+ ret.r -= other.r;
+ ret.g -= other.g;
+ ret.b -= other.b;
+ ret.a -= other.a;
+
+ return ret;
+ }
+
+ ICLR operator+=( const ICLR& other ) {
+ r += other.r;
+ g += other.g;
+ b += other.b;
+ a += other.a;
+
+ return *this;
+ }
+
+ ICLR operator-=( const ICLR& other ) {
+ r -= other.r;
+ g -= other.g;
+ b -= other.b;
+ a -= other.a;
+
+ return *this;
+ }
+
+ static ICLR from_hsb( F32 h, F32 s, F32 b ) {
+ F32 c = s * b;
+ F32 x = c * (1 - abs((int)(h / 60) % 2 - 1));
+ F32 m = b - c;
+
+ F32 _r, _g, _b;
+
+ if (h < 60) {
+ _r = c;
+ _g = x;
+ _b = 0;
+ } else if (h < 120) {
+ _r = x;
+ _g = c;
+ _b = 0;
+ } else if (h < 180) {
+ _r = 0;
+ _g = c;
+ _b = x;
+ } else if (h < 240) {
+ _r = 0;
+ _g = x;
+ _b = c;
+ } else if (h < 300) {
+ _r = x;
+ _g = 0;
+ _b = c;
+ } else {
+ _r = c;
+ _g = 0;
+ _b = x;
+ }
+
+ return {
+ (I32)((_r + m) * 255),
+ (I32)((_g + m) * 255),
+ (I32)((_b + m) * 255),
+ 255
+ };
+ }
+
+ CLR to_byte() {
+ return {
+ (U8)r,
+ (U8)g,
+ (U8)b,
+ (U8)a
+ };
+ }
+
+ FCLR to_float() {
+ return {
+ (F32)r,
+ (F32)g,
+ (F32)b,
+ (F32)a
+ };
+ }
+};
+
+#pragma warning( default: 4244 )