#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 )