Skip to content

Commit 0b8a107

Browse files
authored
Clamp RGB values between 0 and 1 (#25)
Some failures are occuring due to floating point precision where very small negative values are sticking around through XYZ->RGB transformation. This Should correct for that by clamping the RGB values.
1 parent c9fa89b commit 0b8a107

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/Image/Color.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ static Color multiply(const Color LHS, double Mat[9], ColorSpace NewSpace) {
2626
return Color(X, Y, Z, NewSpace);
2727
}
2828

29+
static Color clampColor(const Color C) {
30+
return Color(std::clamp(C.R, 0.0, 1.0), std::clamp(C.G, 0.0, 1.0),
31+
std::clamp(C.B, 0.0, 1.0), C.Space);
32+
}
33+
2934
static Color RGBToXYZ(const Color Old) {
3035
// Matrix assumes D65 white point.
3136
// Source: http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
@@ -39,7 +44,7 @@ static Color XYZToRGB(const Color Old) {
3944
// Source: http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
4045
double Mat[] = {3.2404542, -1.5371385, -0.4985314, -0.9692660, 1.8760108,
4146
0.0415560, 0.0556434, -0.2040259, 1.0572252};
42-
return multiply(Old, Mat, ColorSpace::RGB);
47+
return clampColor(multiply(Old, Mat, ColorSpace::RGB));
4348
}
4449

4550
static double convertXYZ(double Val) {

0 commit comments

Comments
 (0)