OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
ColorSpaceConvert.cpp File Reference
#include "ColorSpaceConvert.hpp"
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <tuple>
#include <math.h>
#include <string>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <wx/colordlg.h>
#include <cmath>
Include dependency graph for ColorSpaceConvert.cpp:

Go to the source code of this file.

Functions

std::tuple< int, int, int > rgb_to_yuv (float r, float g, float b)
double PivotRGB (double n)
double PivotXYZ (double n)
void XYZ2RGB (float X, float Y, float Z, float *R, float *G, float *B)
void Lab2XYZ (float L, float a, float b, float *X, float *Y, float *Z)
void Lab2RGB (float L, float a, float b, float *R, float *G, float *B)
void RGB2XYZ (float R, float G, float B, float *X, float *Y, float *Z)
void XYZ2Lab (float X, float Y, float Z, float *L, float *a, float *b)
void RGB2Lab (float R, float G, float B, float *L, float *a, float *b)
void RGB2HSV (float r, float g, float b, float *h, float *s, float *v)
float DeltaE00 (float l1, float a1, float b1, float l2, float a2, float b2)
float DeltaE94 (float l1, float a1, float b1, float l2, float a2, float b2)
float DeltaE76 (float l1, float a1, float b1, float l2, float a2, float b2)
std::string color_to_string (const wxColour &color)
wxColour string_to_wxColor (const std::string &str)

Variables

static const float param_13 = 1.0f / 3.0f
static const float param_16116 = 16.0f / 116.0f
static const float Xn = 0.950456f
static const float Yn = 1.0f
static const float Zn = 1.088754f

Function Documentation

◆ color_to_string()

std::string color_to_string ( const wxColour & color)
246{
247 std::string str = std::to_string(color.Red()) + "," + std::to_string(color.Green()) + "," + std::to_string(color.Blue()) + "," + std::to_string(color.Alpha());
248 return str;
249}

◆ DeltaE00()

float DeltaE00 ( float l1,
float a1,
float b1,
float l2,
float a2,
float b2 )
149{
150 auto rad2deg = [](float rad) {
151 return 360.0 * rad / (2.0 * M_PI);
152 };
153
154 auto deg2rad = [](float deg) {
155 return (2.0 * M_PI * deg) / 360.0;
156 };
157
158 float avgL = (l1 + l2) / 2.0;
159 float c1 = std::sqrt(std::pow(a1, 2) + std::pow(b1, 2));
160 float c2 = std::sqrt(std::pow(a2, 2) + std::pow(b2, 2));
161 float avgC = (c1 + c2) / 2.0;
162 float g = (1.0 - std::sqrt(std::pow(avgC, 7) / (std::pow(avgC, 7) + std::pow(25.0, 7)))) / 2.0;
163
164 float a1p = a1 * (1.0 + g);
165 float a2p = a2 * (1.0 + g);
166
167 float c1p = std::sqrt(std::pow(a1p, 2) + std::pow(b1, 2));
168 float c2p = std::sqrt(std::pow(a2p, 2) + std::pow(b2, 2));
169
170 float avgCp = (c1p + c2p) / 2.0;
171
172 float h1p = rad2deg(std::atan2(b1, a1p));
173 if (h1p < 0.0) {
174 h1p = h1p + 360.0;
175 }
176
177 float h2p = rad2deg(std::atan2(b2, a2p));
178 if (h2p < 0.0) {
179 h2p = h2p + 360;
180 }
181
182 float avghp = std::abs(h1p - h2p) > 180.0 ? (h1p + h2p + 360.0) / 2.0 : (h1p + h2p) / 2.0;
183
184 float t = 1.0 - 0.17 * std::cos(deg2rad(avghp - 30.0)) + 0.24 * std::cos(deg2rad(2.0 * avghp)) + 0.32 * std::cos(deg2rad(3.0 * avghp + 6.0)) - 0.2 * std::cos(deg2rad(4.0 * avghp - 63.0));
185
186 float deltahp = h2p - h1p;
187 if (std::abs(deltahp) > 180.0) {
188 if (h2p <= h1p) {
189 deltahp += 360.0;
190 }
191 else {
192 deltahp -= 360.0;
193 }
194 }
195
196 float deltalp = l2 - l1;
197 float deltacp = c2p - c1p;
198
199 deltahp = 2.0 * std::sqrt(c1p * c2p) * std::sin(deg2rad(deltahp) / 2.0);
200
201 float sl = 1.0 + ((0.015 * std::pow(avgL - 50.0, 2)) / std::sqrt(20.0 + std::pow(avgL - 50.0, 2)));
202 float sc = 1.0 + 0.045 * avgCp;
203 float sh = 1.0 + 0.015 * avgCp * t;
204
205 float deltaro = 30.0 * std::exp(-(std::pow((avghp - 275.0) / 25.0, 2)));
206 float rc = 2.0 * std::sqrt(std::pow(avgCp, 7) / (std::pow(avgCp, 7) + std::pow(25.0, 7)));
207 float rt = -rc * std::sin(2.0 * deg2rad(deltaro));
208
209 float kl = 1;
210 float kc = 1;
211 float kh = 1;
212
213 float delta_e00 = std::sqrt(std::pow(deltalp / (kl * sl), 2) + std::pow(deltacp / (kc * sc), 2) + std::pow(deltahp / (kh * sh), 2) + rt * (deltacp / (kc * sc)) * (deltahp / (kh * sh)));
214 return delta_e00;
215}
#define M_PI
Definition ExtrusionSimulator.cpp:31
double rad(double deg)
Definition DesignSketchTool.cpp:7748
constexpr T deg2rad(const T angle)
Definition Geometry.hpp:312
T rad2deg(T angle)
Definition Geometry.hpp:310
T l2(const boost::geometry::model::d2::point_xy< T > &v)
Definition ExtrusionSimulator.cpp:177

References M_PI.

Referenced by Slic3r::GUI::GLGizmoTextureDisplacement::make_palette_quantizer(), and Slic3r::GUI::GLGizmoTextureDisplacement::rank_mixes().

Here is the caller graph for this function:

◆ DeltaE76()

float DeltaE76 ( float l1,
float a1,
float b1,
float l2,
float a2,
float b2 )
241{
242 return std::sqrt(std::pow((l1 - l2), 2) + std::pow((a1 - a2), 2) + std::pow((b1 - b2), 2));
243}

Referenced by Slic3r::GUI::calc_color_distance(), and Slic3r::GUI::calc_color_distance().

Here is the caller graph for this function:

◆ DeltaE94()

float DeltaE94 ( float l1,
float a1,
float b1,
float l2,
float a2,
float b2 )
218{
219 float k_L = 1;
220 float k_C = 1;
221 float k_H = 1;
222 float K1 = 0.045;
223 float K2 = 0.015;
224
225 float delta_l = l1 - l2;
226 float C1 = std::sqrt(a1 * a1 + b1 * b1);
227 float C2 = std::sqrt(a2 * a2 + b2 * b2);
228 float delta_c = C1 - C2;
229 float delta_a = a1 - a2;
230 float delta_b = b1 - b2;
231 float delta_h = std::sqrt(delta_a * delta_a + delta_b * delta_b - delta_c * delta_c);
232 float SL = 1.0;
233 float SC = 1 + K1 * C1;
234 float SH = 1 + K2 * C1;
235
236 float delta_e94 = std::sqrt(std::pow(delta_l / (k_L * SL), 2) + std::pow(delta_c / (k_C * SC), 2) + std::pow(delta_h / (k_H / k_C * SH), 2));
237 return delta_e94;
238}

◆ Lab2RGB()

void Lab2RGB ( float L,
float a,
float b,
float * R,
float * G,
float * B )
79{
80 float X = 0.0f, Y = 0.0f, Z = 0.0f;
81 Lab2XYZ(L, a, b, &X, &Y, &Z);
82 XYZ2RGB(X, Y, Z, R, G, B);
83}
void Lab2XYZ(float L, float a, float b, float *X, float *Y, float *Z)
Definition ColorSpaceConvert.cpp:51
void XYZ2RGB(float X, float Y, float Z, float *R, float *G, float *B)
Definition ColorSpaceConvert.cpp:39
#define L(s)
Definition ExtrusionEntity.cpp:25
@ Y
Definition libslic3r.h:142
@ Z
Definition libslic3r.h:143
@ X
Definition libslic3r.h:141

References L, Lab2XYZ(), and XYZ2RGB().

Here is the call graph for this function:

◆ Lab2XYZ()

void Lab2XYZ ( float L,
float a,
float b,
float * X,
float * Y,
float * Z )
52{
53 float fX, fY, fZ;
54
55 fY = (L + 16.0f) / 116.0f;
56 if (fY > 0.206893f)
57 *Y = fY * fY * fY;
58 else
59 *Y = (fY - param_16116) / 7.787f;
60
61 fX = a / 500.0f + fY;
62 if (fX > 0.206893f)
63 *X = fX * fX * fX;
64 else
65 *X = (fX - param_16116) / 7.787f;
66
67 fZ = fY - b / 200.0f;
68 if (fZ > 0.206893f)
69 *Z = fZ * fZ * fZ;
70 else
71 *Z = (fZ - param_16116) / 7.787f;
72
73 (*X) *= Xn;
74 (*Y) *= Yn;
75 (*Z) *= Zn;
76}
static const float Zn
Definition ColorSpaceConvert.cpp:18
static const float param_16116
Definition ColorSpaceConvert.cpp:15
static const float Yn
Definition ColorSpaceConvert.cpp:17
static const float Xn
Definition ColorSpaceConvert.cpp:16

References L, param_16116, Xn, Yn, and Zn.

Referenced by Lab2RGB().

Here is the caller graph for this function:

◆ PivotRGB()

double PivotRGB ( double n)
29{
30 return (n > 0.04045 ? std::pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
31}

Referenced by RGB2XYZ().

Here is the caller graph for this function:

◆ PivotXYZ()

double PivotXYZ ( double n)
34{
35 double i = std::cbrt(n);
36 return n > 0.008856 ? i : 7.787 * n + 16.0 / 116.0;
37}

Referenced by XYZ2Lab().

Here is the caller graph for this function:

◆ RGB2HSV()

void RGB2HSV ( float r,
float g,
float b,
float * h,
float * s,
float * v )
120{
121 float Cmax = std::max(std::max(r, g), b);
122 float Cmin = std::min(std::min(r, g), b);
123 float delta = Cmax - Cmin;
124
125 if (std::abs(delta) < 0.001) {
126 *h = 0.f;
127 }
128 else if (Cmax == r) {
129 *h = 60.f * fmod((g - b) / delta, 6.f);
130 }
131 else if (Cmax == g) {
132 *h = 60.f * ((b - r) / delta + 2);
133 }
134 else {
135 *h = 60.f * ((r - g) / delta + 4);
136 }
137
138 if (std::abs(Cmax) < 0.001) {
139 *s = 0.f;
140 }
141 else {
142 *s = delta / Cmax;
143 }
144
145 *v = Cmax;
146}

Referenced by Slic3r::FlushVolCalculator::calc_flush_vol_rgb().

Here is the caller graph for this function:

◆ RGB2Lab()

void RGB2Lab ( float R,
float G,
float B,
float * L,
float * a,
float * b )
112{
113 float X = 0.0f, Y = 0.0f, Z = 0.0f;
114 RGB2XYZ(R, G, B, &X, &Y, &Z);
115 XYZ2Lab(X, Y, Z, L, a, b);
116}
void RGB2XYZ(float R, float G, float B, float *X, float *Y, float *Z)
Definition ColorSpaceConvert.cpp:85
void XYZ2Lab(float X, float Y, float Z, float *L, float *a, float *b)
Definition ColorSpaceConvert.cpp:96

References L, RGB2XYZ(), and XYZ2Lab().

Referenced by Slic3r::GUI::calc_color_distance(), and Slic3r::GUI::calc_color_distance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RGB2XYZ()

void RGB2XYZ ( float R,
float G,
float B,
float * X,
float * Y,
float * Z )
86{
87 R = PivotRGB(R);
88 G = PivotRGB(G);
89 B = PivotRGB(B);
90
91 *X = 0.412453f * R + 0.357580f * G + 0.180423f * B;
92 *Y = 0.212671f * R + 0.715160f * G + 0.072169f * B;
93 *Z = 0.019334f * R + 0.119193f * G + 0.950227f * B;
94}
double PivotRGB(double n)
Definition ColorSpaceConvert.cpp:28

References PivotRGB().

Referenced by FlushPredict::RGB2LAB(), and RGB2Lab().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rgb_to_yuv()

std::tuple< int, int, int > rgb_to_yuv ( float r,
float g,
float b )
21{
22 int y = (int)(0.2126 * r + 0.7152 * g + 0.0722 * b);
23 int u = (int)(-0.09991 * r - 0.33609 * g + 0.436 * b);
24 int v = (int)(0.615 * r - 0.55861 * g - 0.05639 * b);
25 return std::make_tuple(y, u, v);
26}

◆ string_to_wxColor()

wxColour string_to_wxColor ( const std::string & str)
252{
253 wxColour color;
254 std::vector<std::string> result;
255 boost::split(result, str, boost::is_any_of(","));
256 if (result.size() == 4) {
257 color = wxColour(std::stoi(result[0]), std::stoi(result[1]), std::stoi(result[2]), std::stoi(result[3]));
258 }
259 return color;
260};

◆ XYZ2Lab()

void XYZ2Lab ( float X,
float Y,
float Z,
float * L,
float * a,
float * b )
97{
98 double REF_X = 95.047;
99 double REF_Y = 100.000;
100 double REF_Z = 108.883;
101
102 double x = PivotXYZ(X / REF_X);
103 double y = PivotXYZ(Y / REF_Y);
104 double z = PivotXYZ(Z / REF_Z);
105
106 *L = 116.0 * y - 16.0;
107 *a = 500.0 * (x - y);
108 *b = 200.0 * (y - z);
109}
double PivotXYZ(double n)
Definition ColorSpaceConvert.cpp:33

References L, and PivotXYZ().

Referenced by RGB2Lab().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ XYZ2RGB()

void XYZ2RGB ( float X,
float Y,
float Z,
float * R,
float * G,
float * B )
40{
41 float RR, GG, BB;
42 RR = 3.240479f * X - 1.537150f * Y - 0.498535f * Z;
43 GG = -0.969256f * X + 1.875992f * Y + 0.041556f * Z;
44 BB = 0.055648f * X - 0.204043f * Y + 1.057311f * Z;
45
46 *R = (float)std::clamp(RR, 0.f, 255.f);
47 *G = (float)std::clamp(GG, 0.f, 255.f);
48 *B = (float)std::clamp(BB, 0.f, 255.f);
49}

Referenced by Lab2RGB().

Here is the caller graph for this function:

Variable Documentation

◆ param_13

const float param_13 = 1.0f / 3.0f
static

◆ param_16116

const float param_16116 = 16.0f / 116.0f
static

Referenced by Lab2XYZ().

◆ Xn

const float Xn = 0.950456f
static

◆ Yn

◆ Zn

const float Zn = 1.088754f
static