color_utils.cpp
Go to the documentation of this file.
1 
22 #include <cmath>
23 #include "color_utils.hpp"
24 
25 namespace color_widgets {
26 namespace detail {
27 
28 QColor color_from_lch(qreal hue, qreal chroma, qreal luma, qreal alpha )
29 {
30  qreal h1 = hue*6;
31  qreal x = chroma*(1-qAbs(std::fmod(h1,2)-1));
32  QColor col;
33  if ( h1 >= 0 && h1 < 1 )
34  col = QColor::fromRgbF(chroma,x,0);
35  else if ( h1 < 2 )
36  col = QColor::fromRgbF(x,chroma,0);
37  else if ( h1 < 3 )
38  col = QColor::fromRgbF(0,chroma,x);
39  else if ( h1 < 4 )
40  col = QColor::fromRgbF(0,x,chroma);
41  else if ( h1 < 5 )
42  col = QColor::fromRgbF(x,0,chroma);
43  else if ( h1 < 6 )
44  col = QColor::fromRgbF(chroma,0,x);
45 
46  qreal m = luma - color_lumaF(col);
47 
48  return QColor::fromRgbF(
49  qBound(0.0,col.redF()+m,1.0),
50  qBound(0.0,col.greenF()+m,1.0),
51  qBound(0.0,col.blueF()+m,1.0),
52  alpha);
53 }
54 
55 QColor color_from_hsl(qreal hue, qreal sat, qreal lig, qreal alpha )
56 {
57  qreal chroma = (1 - qAbs(2*lig-1))*sat;
58  qreal h1 = hue*6;
59  qreal x = chroma*(1-qAbs(std::fmod(h1,2)-1));
60  QColor col;
61  if ( h1 >= 0 && h1 < 1 )
62  col = QColor::fromRgbF(chroma,x,0);
63  else if ( h1 < 2 )
64  col = QColor::fromRgbF(x,chroma,0);
65  else if ( h1 < 3 )
66  col = QColor::fromRgbF(0,chroma,x);
67  else if ( h1 < 4 )
68  col = QColor::fromRgbF(0,x,chroma);
69  else if ( h1 < 5 )
70  col = QColor::fromRgbF(x,0,chroma);
71  else if ( h1 < 6 )
72  col = QColor::fromRgbF(chroma,0,x);
73 
74  qreal m = lig-chroma/2;
75 
76  return QColor::fromRgbF(
77  qBound(0.0,col.redF()+m,1.0),
78  qBound(0.0,col.greenF()+m,1.0),
79  qBound(0.0,col.blueF()+m,1.0),
80  alpha);
81 }
82 
83 } // namespace detail
84 } // namespace color_widgets
qreal color_lumaF(const QColor &c)
Definition: color_utils.hpp:36
Definition: chrono.h:284
QColor color_from_lch(qreal hue, qreal chroma, qreal luma, qreal alpha=1)
Definition: color_utils.cpp:28
QColor color_from_hsl(qreal hue, qreal sat, qreal lig, qreal alpha=1)
Definition: color_utils.cpp:55


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:33