File: colorutils.cpp

package info (click to toggle)
musescore 2.3.2%2Bdfsg2-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 187,932 kB
  • sloc: cpp: 264,610; xml: 176,707; sh: 3,311; ansic: 1,384; python: 356; makefile: 188; perl: 82; pascal: 78
file content (265 lines) | stat: -rw-r--r-- 8,114 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//=============================================================================
//  MuseScore
//  Linux Music Score Editor
//  $Id:$
//
//  Copyright (C) 2010 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include <math.h>
#include "colorutils.h"

//---------------------------------------------------------
//   ColorUtils
//---------------------------------------------------------

static inline qreal wrap(qreal a, qreal d = 1.0) {
      qreal r = fmod(a, d);
      return (r < 0.0 ? d + r : (r > 0.0 ? r : 0.0));
      }

//---------------------------------------------------------
//   KHCY
//---------------------------------------------------------

KHCY::KHCY(qreal h_, qreal c_, qreal y_, qreal a_) {
      h = h_;
      c = c_;
      y = y_;
      a = a_;
      }

KHCY::KHCY(const QColor& color) {
      qreal r = gamma(color.redF());
      qreal g = gamma(color.greenF());
      qreal b = gamma(color.blueF());
      a = color.alphaF();

      // luma component
      y = lumag(r, g, b);

      // hue component
      qreal p = qMax(qMax(r, g), b);
      qreal n = qMin(qMin(r, g), b);
      qreal d = 6.0 * (p - n);
      if (n == p)
            h = 0.0;
      else if (r == p)
            h = ((g - b) / d);
      else if (g == p)
            h = ((b - r) / d) + (1.0 / 3.0);
      else
            h = ((r - g) / d) + (2.0 / 3.0);

      // chroma component
      if (r == g && g == b)
            c = 0.0;
      else
            c = qMax( (y - n) / y, (p - y) / (1 - y) );
      }

//---------------------------------------------------------
//   qColor
//---------------------------------------------------------

QColor KHCY::qColor() const {
      // start with sane component values
      qreal _h = wrap(h);
      qreal _c = normalize(c);
      qreal _y = normalize(y);

      // calculate some needed variables
      qreal _hs = _h * 6.0, th, tm;
      if (_hs < 1.0) {
            th = _hs;
            tm = yc[0] + yc[1] * th;
            }
      else if (_hs < 2.0) {
            th = 2.0 - _hs;
            tm = yc[1] + yc[0] * th;
            }
      else if (_hs < 3.0) {
            th = _hs - 2.0;
            tm = yc[1] + yc[2] * th;
            }
      else if (_hs < 4.0) {
            th = 4.0 - _hs;
            tm = yc[2] + yc[1] * th;
            }
      else if (_hs < 5.0) {
            th = _hs - 4.0;
            tm = yc[2] + yc[0] * th;
            }
      else {
            th = 6.0 - _hs;
            tm = yc[0] + yc[2] * th;
            }

      // calculate RGB channels in sorted order
      qreal tn, to, tp;
      if (tm >= _y) {
            tp = _y + _y * _c * (1.0 - tm) / tm;
            to = _y + _y * _c * (th - tm) / tm;
            tn = _y - (_y * _c);
            }
      else {
            tp = _y + (1.0 - _y) * _c;
            to = _y + (1.0 - _y) * _c * (th - tm) / (1.0 - tm);
            tn = _y - (1.0 - _y) * _c * tm / (1.0 - tm);
            }

      // return RGB channels in appropriate order
      if (_hs < 1.0)
            return QColor::fromRgbF(igamma(tp), igamma(to), igamma(tn), a);
      else if (_hs < 2.0)
            return QColor::fromRgbF(igamma(to), igamma(tp), igamma(tn), a);
      else if (_hs < 3.0)
            return QColor::fromRgbF(igamma(tn), igamma(tp), igamma(to), a);
      else if (_hs < 4.0)
            return QColor::fromRgbF(igamma(tn), igamma(to), igamma(tp), a);
      else if (_hs < 5.0)
            return QColor::fromRgbF(igamma(to), igamma(tn), igamma(tp), a);
      else
            return QColor::fromRgbF(igamma(tp), igamma(tn), igamma(to), a);
      }

//---------------------------------------------------------
//   luma
//---------------------------------------------------------

qreal KHCY::luma(const QColor& color) {
      return lumag(gamma(color.redF()), gamma(color.greenF()), gamma(color.blueF()));
      }

//---------------------------------------------------------
//   mixQreal
//---------------------------------------------------------

static inline qreal mixQreal(qreal a, qreal b, qreal bias) {
      return a + (b - a) * bias;
      }

//---------------------------------------------------------
//   tintHelper
//---------------------------------------------------------

QColor ColorUtils::tintHelper(const QColor& base, const QColor& color, qreal amount) {
      KHCY result(ColorUtils::mix(base, color, pow(amount, 0.3)));
      result.y = mixQreal(ColorUtils::luma(base), result.y, amount);
      return result.qColor();
      }

//---------------------------------------------------------
//   contrastRatio
//---------------------------------------------------------

qreal ColorUtils::contrastRatio(const QColor& c1, const QColor& c2) {
      qreal y1 = luma(c1), y2 = luma(c2);
      if (y1 > y2)
            return (y1 + 0.05) / (y2 + 0.05);
      else
            return (y2 + 0.05) / (y1 + 0.05);
      }

//---------------------------------------------------------
//   tint
//---------------------------------------------------------

QColor ColorUtils::tint(const QColor& base, const QColor& color, qreal amount) {
      if (amount <= 0.0)
            return base;
      if (amount >= 1.0)
            return color;
#ifdef Q_OS_MAC
      if (isnan(amount))
#else      
      if (std::isnan(amount))
#endif
            return base;

      double ri = contrastRatio(base, color);
      double rg = 1.0 + ((ri + 1.0) * amount * amount * amount);
      double u = 1.0, l = 0.0;
      QColor result;
      for (int i = 12 ; i ; --i) {
            double a = 0.5 * (l + u);
            result = tintHelper(base, color, a);
            double ra = contrastRatio(base, result);
            if (ra > rg)
                  u = a;
            else
                  l = a;
            }
      return result;
      }

//---------------------------------------------------------
//   mix
//---------------------------------------------------------

QColor ColorUtils::mix(const QColor& c1, const QColor& c2, qreal bias) {
      if (bias <= 0.0)
            return c1;
      if (bias >= 1.0)
            return c2;
#ifdef Q_OS_MAC
      if (isnan(bias))
#else      
      if (std::isnan(bias))
#endif
            return c1;

      qreal r = mixQreal(c1.redF(),   c2.redF(),   bias);
      qreal g = mixQreal(c1.greenF(), c2.greenF(), bias);
      qreal b = mixQreal(c1.blueF(),  c2.blueF(),  bias);
      qreal a = mixQreal(c1.alphaF(), c2.alphaF(), bias);

      return QColor::fromRgbF(r, g, b, a);
      }

//---------------------------------------------------------
//   lighten
//---------------------------------------------------------

QColor ColorUtils::lighten(const QColor& color, qreal ky, qreal kc) {
      KHCY c(color);
      c.y = 1.0 - normalize((1.0 - c.y) * (1.0 - ky));
      c.c = 1.0 - normalize((1.0 - c.c) * kc);
      return c.qColor();
      }

//---------------------------------------------------------
//   darken
//---------------------------------------------------------

QColor ColorUtils::darken(const QColor& color, qreal ky, qreal kc) {
      KHCY c(color);
      c.y = normalize(c.y * (1.0 - ky));
      c.c = normalize(c.c * kc);
      return c.qColor();
      }

//---------------------------------------------------------
//   shade
//---------------------------------------------------------

QColor ColorUtils::shade(const QColor& color, qreal ky, qreal kc) {
      KHCY c(color);
      c.y = normalize(c.y + ky);
      c.c = normalize(c.c + kc);
      return c.qColor();
      }