• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • colors
kcolorspaces.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  * Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
3  * Copyright (C) 2007 Olaf Schmidt <ojschmidt@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #include "kcolorspaces.h"
21 #include "kcolorhelpers_p.h"
22 
23 #include <QColor>
24 
25 #include <math.h>
26 
27 using namespace KColorSpaces;
28 
29 static inline qreal wrap(qreal a, qreal d = 1.0)
30 {
31  qreal r = fmod(a, d);
32  return (r < 0.0 ? d + r : (r > 0.0 ? r : 0.0));
33 }
34 
36 // HCY color space
37 
38 #define HCY_REC 709 // use 709 for now
39 #if HCY_REC == 601
40 static const qreal yc[3] = { 0.299, 0.587, 0.114 };
41 #elif HCY_REC == 709
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 };
45 #endif
46 
47 qreal KHCY::gamma(qreal n)
48 {
49  return pow(normalize(n), 2.2);
50 }
51 
52 qreal KHCY::igamma(qreal n)
53 {
54  return pow(normalize(n), 1.0/2.2);
55 }
56 
57 qreal KHCY::lumag(qreal r, qreal g, qreal b)
58 {
59  return r*yc[0] + g*yc[1] + b*yc[2];
60 }
61 
62 KHCY::KHCY(qreal h_, qreal c_, qreal y_, qreal a_)
63 {
64  h = h_;
65  c = c_;
66  y = y_;
67  a = a_;
68 }
69 
70 KHCY::KHCY(const QColor& color)
71 {
72  qreal r = gamma(color.redF());
73  qreal g = gamma(color.greenF());
74  qreal b = gamma(color.blueF());
75  a = color.alphaF();
76 
77  // luma component
78  y = lumag(r, g, b);
79 
80  // hue component
81  qreal p = qMax(qMax(r, g), b);
82  qreal n = qMin(qMin(r, g), b);
83  qreal d = 6.0 * (p - n);
84  if (n == p)
85  h = 0.0;
86  else if (r == p)
87  h = ((g - b) / d);
88  else if (g == p)
89  h = ((b - r) / d) + (1.0 / 3.0);
90  else
91  h = ((r - g) / d) + (2.0 / 3.0);
92 
93  // chroma component
94  if (r == g && g == b)
95  c = 0.0;
96  else
97  c = qMax( (y - n) / y, (p - y) / (1 - y) );
98 }
99 
100 QColor KHCY::qColor() const
101 {
102  // start with sane component values
103  qreal _h = wrap(h);
104  qreal _c = normalize(c);
105  qreal _y = normalize(y);
106 
107  // calculate some needed variables
108  qreal _hs = _h * 6.0, th, tm;
109  if (_hs < 1.0) {
110  th = _hs;
111  tm = yc[0] + yc[1] * th;
112  }
113  else if (_hs < 2.0) {
114  th = 2.0 - _hs;
115  tm = yc[1] + yc[0] * th;
116  }
117  else if (_hs < 3.0) {
118  th = _hs - 2.0;
119  tm = yc[1] + yc[2] * th;
120  }
121  else if (_hs < 4.0) {
122  th = 4.0 - _hs;
123  tm = yc[2] + yc[1] * th;
124  }
125  else if (_hs < 5.0) {
126  th = _hs - 4.0;
127  tm = yc[2] + yc[0] * th;
128  }
129  else {
130  th = 6.0 - _hs;
131  tm = yc[0] + yc[2] * th;
132  }
133 
134  // calculate RGB channels in sorted order
135  qreal tn, to, tp;
136  if (tm >= _y) {
137  tp = _y + _y * _c * (1.0 - tm) / tm;
138  to = _y + _y * _c * (th - tm) / tm;
139  tn = _y - (_y * _c);
140  }
141  else {
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);
145  }
146 
147  // return RGB channels in appropriate order
148  if (_hs < 1.0)
149  return QColor::fromRgbF(igamma(tp), igamma(to), igamma(tn), a);
150  else if (_hs < 2.0)
151  return QColor::fromRgbF(igamma(to), igamma(tp), igamma(tn), a);
152  else if (_hs < 3.0)
153  return QColor::fromRgbF(igamma(tn), igamma(tp), igamma(to), a);
154  else if (_hs < 4.0)
155  return QColor::fromRgbF(igamma(tn), igamma(to), igamma(tp), a);
156  else if (_hs < 5.0)
157  return QColor::fromRgbF(igamma(to), igamma(tn), igamma(tp), a);
158  else
159  return QColor::fromRgbF(igamma(tp), igamma(tn), igamma(to), a);
160 }
161 
162 qreal KHCY::luma(const QColor& color)
163 {
164  return lumag(gamma(color.redF()),
165  gamma(color.greenF()),
166  gamma(color.blueF()));
167 }
168 
169 // kate: space-indent on; indent-width 4; replace-tabs on; auto-insert-doxygen on;
wrap
static qreal wrap(qreal a, qreal d=1.0)
Definition: kcolorspaces.cpp:29
KColorSpaces::KHCY::luma
static qreal luma(const QColor &)
Definition: kcolorspaces.cpp:162
QColor::alphaF
qreal alphaF() const
QColor::redF
qreal redF() const
KColorSpaces::KHCY::KHCY
KHCY(const QColor &)
Definition: kcolorspaces.cpp:70
QColor::blueF
qreal blueF() const
yc
static const qreal yc[3]
Definition: kcolorspaces.cpp:42
KColorSpaces::KHCY::c
qreal c
Definition: kcolorspaces.h:40
KColorSpaces::KHCY::h
qreal h
Definition: kcolorspaces.h:40
QColor::greenF
qreal greenF() const
KColorSpaces::KHCY::a
qreal a
Definition: kcolorspaces.h:40
QColor
QColor::fromRgbF
QColor fromRgbF(qreal r, qreal g, qreal b, qreal a)
KColorSpaces::KHCY::qColor
QColor qColor() const
Definition: kcolorspaces.cpp:100
KColorSpaces::KHCY::y
qreal y
Definition: kcolorspaces.h:40
kcolorspaces.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal