21 #include "kcolorhelpers_p.h"
27 using namespace KColorSpaces;
29 static inline qreal
wrap(qreal a, qreal d = 1.0)
32 return (r < 0.0 ? d + r : (r > 0.0 ? r : 0.0));
38 #define HCY_REC 709 // use 709 for now
40 static const qreal
yc[3] = { 0.299, 0.587, 0.114 };
42 static const qreal
yc[3] = {0.2126, 0.7152, 0.0722};
43 #else // use Qt values
44 static const qreal
yc[3] = { 0.34375, 0.5, 0.15625 };
47 qreal KHCY::gamma(qreal n)
49 return pow(normalize(n), 2.2);
52 qreal KHCY::igamma(qreal n)
54 return pow(normalize(n), 1.0/2.2);
57 qreal KHCY::lumag(qreal r, qreal g, qreal b)
59 return r*
yc[0] + g*
yc[1] + b*
yc[2];
72 qreal r = gamma(color.redF());
73 qreal g = gamma(color.greenF());
74 qreal b = gamma(color.blueF());
81 qreal p = qMax(qMax(r, g), b);
82 qreal n = qMin(qMin(r, g), b);
83 qreal d = 6.0 * (p - n);
89 h = ((b - r) / d) + (1.0 / 3.0);
91 h = ((r - g) / d) + (2.0 / 3.0);
97 c = qMax( (
y - n) /
y, (p -
y) / (1 -
y) );
104 qreal _c = normalize(
c);
105 qreal _y = normalize(
y);
108 qreal _hs = _h * 6.0, th, tm;
111 tm =
yc[0] +
yc[1] * th;
113 else if (_hs < 2.0) {
115 tm =
yc[1] +
yc[0] * th;
117 else if (_hs < 3.0) {
119 tm =
yc[1] +
yc[2] * th;
121 else if (_hs < 4.0) {
123 tm =
yc[2] +
yc[1] * th;
125 else if (_hs < 5.0) {
127 tm =
yc[2] +
yc[0] * th;
131 tm =
yc[0] +
yc[2] * th;
137 tp = _y + _y * _c * (1.0 - tm) / tm;
138 to = _y + _y * _c * (th - tm) / tm;
142 tp = _y + (1.0 - _y) * _c;
143 to = _y + (1.0 - _y) * _c * (th - tm) / (1.0 - tm);
144 tn = _y - (1.0 - _y) * _c * tm / (1.0 - tm);
149 return QColor::fromRgbF(igamma(tp), igamma(to), igamma(tn),
a);
151 return QColor::fromRgbF(igamma(to), igamma(tp), igamma(tn),
a);
153 return QColor::fromRgbF(igamma(tn), igamma(tp), igamma(to),
a);
155 return QColor::fromRgbF(igamma(tn), igamma(to), igamma(tp),
a);
157 return QColor::fromRgbF(igamma(to), igamma(tn), igamma(tp),
a);
159 return QColor::fromRgbF(igamma(tp), igamma(tn), igamma(to),
a);
164 return lumag(gamma(color.redF()),
165 gamma(color.greenF()),
166 gamma(color.blueF()));
static qreal wrap(qreal a, qreal d=1.0)
static qreal luma(const QColor &)