1 /**
2 * \file
3 *
4 * \author Mattia Basaglia
5 *
6 * \copyright Copyright (C) 2013-2017 Mattia Basaglia
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22 #include "color_utils.hpp"
23
24 namespace color_widgets {
25 namespace detail {
26
color_from_lch(qreal hue,qreal chroma,qreal luma,qreal alpha)27 QColor color_from_lch(qreal hue, qreal chroma, qreal luma, qreal alpha)
28 {
29 qreal h1 = hue * 6;
30 qreal x = chroma * (1 - qAbs(std::fmod(h1, 2) - 1));
31 QColor col;
32 if (h1 >= 0 && h1 < 1)
33 col = QColor::fromRgbF(chroma, x, 0);
34 else if (h1 < 2)
35 col = QColor::fromRgbF(x, chroma, 0);
36 else if (h1 < 3)
37 col = QColor::fromRgbF(0, chroma, x);
38 else if (h1 < 4)
39 col = QColor::fromRgbF(0, x, chroma);
40 else if (h1 < 5)
41 col = QColor::fromRgbF(x, 0, chroma);
42 else if (h1 < 6)
43 col = QColor::fromRgbF(chroma, 0, x);
44
45 qreal m = luma - color_lumaF(col);
46
47 return QColor::fromRgbF(qBound(0.0, col.redF() + m, 1.0),
48 qBound(0.0, col.greenF() + m, 1.0),
49 qBound(0.0, col.blueF() + m, 1.0),
50 alpha);
51 }
52
color_from_hsl(qreal hue,qreal sat,qreal lig,qreal alpha)53 QColor color_from_hsl(qreal hue, qreal sat, qreal lig, qreal alpha)
54 {
55 qreal chroma = (1 - qAbs(2 * lig - 1)) * sat;
56 qreal h1 = hue * 6;
57 qreal x = chroma * (1 - qAbs(std::fmod(h1, 2) - 1));
58 QColor col;
59 if (h1 >= 0 && h1 < 1)
60 col = QColor::fromRgbF(chroma, x, 0);
61 else if (h1 < 2)
62 col = QColor::fromRgbF(x, chroma, 0);
63 else if (h1 < 3)
64 col = QColor::fromRgbF(0, chroma, x);
65 else if (h1 < 4)
66 col = QColor::fromRgbF(0, x, chroma);
67 else if (h1 < 5)
68 col = QColor::fromRgbF(x, 0, chroma);
69 else if (h1 < 6)
70 col = QColor::fromRgbF(chroma, 0, x);
71
72 qreal m = lig - chroma / 2;
73
74 return QColor::fromRgbF(qBound(0.0, col.redF() + m, 1.0),
75 qBound(0.0, col.greenF() + m, 1.0),
76 qBound(0.0, col.blueF() + m, 1.0),
77 alpha);
78 }
79
80 } // namespace detail
81 } // namespace color_widgets
82