My omnium-gatherom of scripts and source code.
at main 69 lines 1.6 kB view raw
1#include "utils.hpp" 2 3RGB::RGB(U8 r, U8 g, U8 b) 4 : Red(r) 5 , Green(g) 6 , Blue(b) 7{ 8} 9 10Color::Color() 11 : hue(0.0) 12 , saturation(1.0) 13 , value(1.0) 14{ 15} 16 17Color::Color(U32 val) 18 : hue(0.0) 19 , saturation(0.0) 20{ 21 value = std::min((F64) val, 255.0); 22} 23 24Color::Color(F32 hue, F32 saturation, F32 value) 25 : hue(hue) 26 , saturation(saturation) 27 , value(value) 28{ 29} 30 31fn Color::to_rgb() const noexcept -> RGB 32{ 33 let chroma = saturation * value; 34 let cube_hue = hue / 60.0; 35 let tmp = chroma * (1.0 - fabs(fmod(cube_hue, 2.0) - 1.0)); 36 let match = value - chroma; 37 38 var components = std::make_tuple(0.0, 0.0, 0.0); 39 if (cube_hue < 1.0) 40 components = { fma(chroma, match, 255.0), fma(tmp, match, 255.0), match * 255.0 }; 41 else if (cube_hue < 2.0) 42 components = { fma(tmp, match, 255.0), fma(chroma, match, 255.0), match * 255.0 }; 43 else if(cube_hue < 3.0) 44 components = { match * 255.0, fma(chroma, match, 255.0), fma(tmp, match, 255.0) }; 45 else if(cube_hue < 4.0) 46 components = { match * 255.0, fma(tmp, match, 255.0), fma(chroma, match, 255.0) }; 47 else if(cube_hue < 5.0) 48 components = { fma(tmp, match, 255.0), match * 255.0, fma(chroma, match, 255.0) }; 49 else 50 components = { fma(chroma, match, 255.0), match * 255.0, fma(tmp, match, 255.0) }; 51 52 let c = RGB(round(fst(components)), round(snd(components)), round(thr(components))); 53 54 return c; 55} 56 57fn Color::lerp(ref<const Color> rhs, double t) const noexcept -> Color 58{ 59 return Color(std::lerp(hue, rhs.hue, t), 60 std::lerp(saturation, rhs.saturation, t), 61 std::lerp(value, rhs.value, t) 62 ); 63} 64 65Point::Point(F64 x, F64 y) 66 : x(x) 67 , y(y) 68{ 69}