libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ciepoint.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cmath>
11#include <array>
13#include <promeki/core/point.h>
15
17
26class CIEPoint {
27 public:
30
32 static constexpr double MinWavelength = 360;
33
35 static constexpr double MaxWavelength = 700;
36
37 //static const CIEPoint D50;
38 //static const CIEPoint D55;
39
45 static bool isValidWavelength(double val) {
46 return val >= MinWavelength && val <= MaxWavelength;
47 }
48
54 static XYZColor wavelengthToXYZ(double wavelength);
55
61 static CIEPoint wavelengthToCIEPoint(double wavelength);
62
73 // from http://www.brucelindbloom.com/index.html?Eqn_T_to_xy.html
74 double x, y;
75 if(cct < 4000) {
76 return CIEPoint(); // Invalid.
77 } else if(cct <= 7000.0) {
78 x = (-4.6070e9 / std::pow(cct, 3)) +
79 ( 2.9678e6 / std::pow(cct, 2)) +
80 ( 0.09911e3 / cct) + 0.244063;
81 } else if(cct <= 25000) {
82 x = (-2.0064e9 / std::pow(cct, 3)) +
83 ( 1.9108e6 / std::pow(cct, 2)) +
84 ( 0.24748e3 / cct) + 0.237040;
85 } else {
86 return CIEPoint(); // Invalid
87 }
88 y = (-3.0 * std::pow(x, 2)) + (2.870 * x) - 0.275;
89 return CIEPoint(x, y);
90 }
91
97 CIEPoint(double x = -1.0, double y = -1.0) : d(x, y) { }
98
103 CIEPoint(const DataType &other) : d(other) { }
104
109 bool isValid() const {
110 return d.isBetween(DataType(0.0, 0.0), DataType(0.8, 0.9));
111 }
112
119 CIEPoint lerp(const CIEPoint &other, double t) const {
120 return d.lerp(other.d, t);
121 }
122#if 0
123 XYZ toXYZ(double Y = 1.0) const {
124 double X = (x() * Y) / y();
125 double Z = ((1.0 - x() - y()) * Y) / y();
126 return {X, Y, Z};
127 }
128
129 bool isInGamut() const {
130 auto [X, Y, Z] = toXYZ();
131 double u = (4.0 * X) / (-2.0 * X + 12.0 * Y + 3.0 * Z);
132 double v = (9.0 * Y) / (-2.0 * X + 12.0 * Y + 3.0 * Z);
133 return u >= 0 && u <= 0.6 && v >= 0 && v <= 0.6 && u + v <= 0.6;
134 }
135
136 double colorTemp() const {
137 // McCamy's approximation
138 double n = (x() - 0.3320) / (0.1858 - y());
139 return 437.0 * std::pow(n, 3.0) +
140 3601.0 * std::pow(n, 2.0) +
141 6861.0 * n + 5517;
142 }
143#endif
144 private:
145 DataType d;
146
147};
148
150
bool isBetween(const Array< T, NumValues > &min, const Array< T, NumValues > &max) const
Returns true if all elements fall within the given range.
Definition array.h:338
Array< T, NumValues > lerp(const Array< T, NumValues > &other, double v) const
Returns a linearly interpolated array between this one and another.
Definition array.h:310
A point in the CIE 1931 xy chromaticity diagram.
Definition ciepoint.h:26
static constexpr double MaxWavelength
Maximum visible wavelength in nanometers.
Definition ciepoint.h:35
CIEPoint(double x=-1.0, double y=-1.0)
Constructs a CIEPoint with the given chromaticity coordinates.
Definition ciepoint.h:97
static constexpr double MinWavelength
Minimum visible wavelength in nanometers.
Definition ciepoint.h:32
static XYZColor wavelengthToXYZ(double wavelength)
Converts a wavelength to a CIE XYZ color.
static CIEPoint colorTempToWhitePoint(double cct)
Computes the white point for a correlated color temperature.
Definition ciepoint.h:72
static bool isValidWavelength(double val)
Checks whether a wavelength is within the visible range.
Definition ciepoint.h:45
CIEPoint(const DataType &other)
Constructs a CIEPoint from raw coordinate data.
Definition ciepoint.h:103
bool isValid() const
Checks whether this point lies within valid chromaticity bounds.
Definition ciepoint.h:109
static CIEPoint wavelengthToCIEPoint(double wavelength)
Converts a wavelength to a CIE xy chromaticity point.
CIEPoint lerp(const CIEPoint &other, double t) const
Linearly interpolates between this point and another.
Definition ciepoint.h:119
Array< double, 2 > DataType
Underlying storage type for the x and y coordinates.
Definition ciepoint.h:29
Dynamic array container wrapping std::vector.
Definition list.h:40
CIE XYZ color value.
Definition xyzcolor.h:25
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19