CGVIS Utils
Loading...
Searching...
No Matches
ColorConversion.h
Go to the documentation of this file.
1#pragma once
2
3#include "Vec3.h"
4#include "Vec4.h"
5#include "Mat4.h"
6
21namespace ColorConversion {
28 template <typename T> Vec3t<T> rgbToHsv(const Vec3t<T>& other) {
29 const T minComp = std::min(other.r, std::min(other.g,other.b));
30 const T maxComp = std::max(other.r, std::max(other.g,other.b));
31 const T delta = maxComp - minComp;
32
33 T h = T(0);
34 if (maxComp != minComp) {
35 if (maxComp == other.r)
36 h = fmod((T(60) * ((other.g - other.b) / delta) + T(360)), T(360));
37 else if (maxComp == other.g)
38 h = fmod((T(60) * ((other.b - other.r) / delta) + T(120)), T(360));
39 else if (maxComp == other.b)
40 h = fmod((T(60) * ((other.r - other.g) / delta) + T(240)), T(360));
41 }
42
43 const T s = (maxComp == T(0)) ? T(0) : (delta / maxComp);
44 const T v = maxComp;
45
46 return {h,s,v};
47 }
48
56 template <typename T> Vec3t<T> hsvToRgb(const Vec3t<T>& other) {
57 const T h = T(int32_t(other.x) % 360) / T(60);
58 const T s = std::max(T(0), std::min(T(1), other.y));
59 const T v = std::max(T(0), std::min(T(1), other.z));
60
61 if (s == 0) return {v,v,v}; // Achromatic (grey)
62
63 const int32_t i = int32_t(floor(h));
64 const T f = h - T(i);
65 const T p = v * (T(1) - s);
66 const T q = v * (T(1) - s * f);
67 const T t = v * (T(1) - s * (T(1) - f));
68
69 switch(i) {
70 case 0: return {v,t,p};
71 case 1: return {q,v,p};
72 case 2: return {p,v,t};
73 case 3: return {p,q,v};
74 case 4: return {t,p,v};
75 default: return {v,p,q};
76 }
77 }
78
85 template <typename T> Vec3t<T> hslToHsv(const Vec3t<T>& other) {
86 const T h = other.x;
87 const T s = other.y;
88 const T l = other.z;
89
90 const T v = s*std::min(l,T(1)-l)+l;
91 return {h,(v > T(0)) ? T(2)-T(2)*l/v : T(0), v};
92 }
93
100 template <typename T> Vec3t<T> hsvToHsl(const Vec3t<T>& other) {
101 const T h = other.x;
102 const T s = other.y;
103 const T v = other.z;
104
105 T l = v-v*s/T(2);
106 T m = std::min(l,T(1)-l);
107 return {h,(m > T(0)) ? (v-l)/m : l, l};
108 }
109
116 template <typename T> Vec3t<T> rgbToCmy(const Vec3t<T>& other) {
117 return {T(1)-other.x,T(1)-other.y,T(1)-other.z};
118 }
119
126 template <typename T> Vec4t<T> rgbToCmyk(const Vec3t<T>& other) {
127 const Vec3t<T> cmy = rgbToCmy(other);
128 const T minVal = std::min(cmy.x,std::min(cmy.y,cmy.z));
129 return {cmy-minVal,minVal};
130 }
131
138 template <typename T> Vec3t<T> cmyToRgb(const Vec3t<T>& other){
139 return {T(1)-other.x,T(1)-other.y,T(1)-other.z};
140 }
141
148 template <typename T> Vec3t<T> cmykToRgb(const Vec4& other) {
149 return {T(1)-(other.x+other.w),T(1)-(other.y+other.w),T(1)-(other.z+other.w)};
150 }
151
158 template <typename T> Vec3t<T> rgbToYuv(const Vec3t<T>& other) {
159 const Mat4t<T> c{ T(0.299), T(0.587), T(0.114), T(0),
160 T(-0.147), T(-0.289), T(0.436), T(0),
161 T(0.615), T(-0.515), T(-0.100), T(0),
162 T(0), T(0), T(0), T(1)
163 };
164 return (c * Vec4t<T>(other,1)).xyz;
165 }
166
173 template <typename T> Vec3t<T> yuvToRgb(const Vec3t<T>& other) {
174 const Mat4t<T> c{ T(1), T(0), T(1.140), T(0),
175 T(1), T(-0.395), T(-0.581), T(0),
176 T(1), T(2.032), T(0), T(0),
177 T(0), T(0), T(0), T(1)
178 };
179 return (c * Vec4t<T>(other,1)).xyz;
180 }
181}
4×4 matrix template for 3D transformations, projections, and stereo sets.
3D vector template with arithmetic, geometry helpers, and RNG utilities.
4D vector template with arithmetic, length helpers, and utilities.
Vec4t< float > Vec4
4D float vector (x,y,z,w).
Definition Vec4.h:337
Definition Mat4.h:29
Definition ColorConversion.h:21
Vec3t< T > hsvToHsl(const Vec3t< T > &other)
Convert HSV to HSL.
Definition ColorConversion.h:100
Vec3t< T > rgbToHsv(const Vec3t< T > &other)
Convert linear RGB to HSV.
Definition ColorConversion.h:28
Vec4t< T > rgbToCmyk(const Vec3t< T > &other)
Convert RGB to CMYK.
Definition ColorConversion.h:126
Vec3t< T > rgbToYuv(const Vec3t< T > &other)
Convert RGB to a simple YUV (luma/chroma) space via a linear matrix.
Definition ColorConversion.h:158
Vec3t< T > hsvToRgb(const Vec3t< T > &other)
Convert HSV to linear RGB.
Definition ColorConversion.h:56
Vec3t< T > rgbToCmy(const Vec3t< T > &other)
Convert RGB to CMY.
Definition ColorConversion.h:116
Vec3t< T > hslToHsv(const Vec3t< T > &other)
Convert HSL to HSV.
Definition ColorConversion.h:85
Vec3t< T > cmyToRgb(const Vec3t< T > &other)
Convert CMY to RGB.
Definition ColorConversion.h:138
Vec3t< T > cmykToRgb(const Vec4 &other)
Convert CMYK to RGB.
Definition ColorConversion.h:148
Vec3t< T > yuvToRgb(const Vec3t< T > &other)
Convert the simple YUV (above) back to RGB.
Definition ColorConversion.h:173
Definition Vec3.h:29
T y
Definition Vec3.h:33
T x
Definition Vec3.h:33
T g
Definition Vec3.h:34
T z
Definition Vec3.h:33
T r
Definition Vec3.h:34
T b
Definition Vec3.h:34
Definition Vec4.h:25
T z
Definition Vec4.h:29
T w
Definition Vec4.h:29
T y
Definition Vec4.h:29
T x
Definition Vec4.h:29