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

KDEUI

  • kdeui
  • colors
kcolorutils.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 Thomas Zander <zander@kde.org>
4 * Copyright (C) 2007 Zack Rusin <zack@kde.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21#include <kcolorutils.h>
22#include "kcolorspaces.h"
23#include "kcolorhelpers_p.h"
24
25#include <QColor>
26#include <QImage>
27
28#include <math.h>
29
30// BEGIN internal helper functions
31static inline qreal mixQreal(qreal a, qreal b, qreal bias)
32{
33 return a + (b - a) * bias;
34}
35// END internal helper functions
36
37qreal KColorUtils::luma(const QColor &color)
38{
39 return KColorSpaces::KHCY::luma(color);
40}
41
42static qreal contrastRatioForLuma(qreal y1, qreal y2)
43{
44 if (y1 > y2)
45 return (y1 + 0.05) / (y2 + 0.05);
46 else
47 return (y2 + 0.05) / (y1 + 0.05);
48}
49
50qreal KColorUtils::contrastRatio(const QColor &c1, const QColor &c2)
51{
52 return contrastRatioForLuma(luma(c1), luma(c2));
53}
54
55QColor KColorUtils::lighten(const QColor &color, qreal ky, qreal kc)
56{
57 KColorSpaces::KHCY c(color);
58 c.y = 1.0 - normalize((1.0 - c.y) * (1.0 - ky));
59 c.c = 1.0 - normalize((1.0 - c.c) * kc);
60 return c.qColor();
61}
62
63QColor KColorUtils::darken(const QColor &color, qreal ky, qreal kc)
64{
65 KColorSpaces::KHCY c(color);
66 c.y = normalize(c.y * (1.0 - ky));
67 c.c = normalize(c.c * kc);
68 return c.qColor();
69}
70
71QColor KColorUtils::shade(const QColor &color, qreal ky, qreal kc)
72{
73 KColorSpaces::KHCY c(color);
74 c.y = normalize(c.y + ky);
75 c.c = normalize(c.c + kc);
76 return c.qColor();
77}
78
79static QColor tintHelper(const QColor &base, qreal baseLuma, const QColor &color, qreal amount)
80{
81 KColorSpaces::KHCY result(KColorUtils::mix(base, color, pow(amount, 0.3)));
82 result.y = mixQreal(baseLuma, result.y, amount);
83
84 return result.qColor();
85}
86
87QColor KColorUtils::tint(const QColor &base, const QColor &color, qreal amount)
88{
89 if (amount <= 0.0) return base;
90 if (amount >= 1.0) return color;
91 if (isnan(amount)) return base;
92
93 qreal baseLuma = luma(base); //cache value because luma call is expensive
94 double ri = contrastRatioForLuma(baseLuma, luma(color));
95 double rg = 1.0 + ((ri + 1.0) * amount * amount * amount);
96 double u = 1.0, l = 0.0;
97 QColor result;
98 for (int i = 12 ; i ; --i) {
99 double a = 0.5 * (l+u);
100 result = tintHelper(base, baseLuma, color, a);
101 double ra = contrastRatioForLuma(baseLuma, luma(result));
102 if (ra > rg)
103 u = a;
104 else
105 l = a;
106 }
107 return result;
108}
109
110QColor KColorUtils::mix(const QColor &c1, const QColor &c2, qreal bias)
111{
112 if (bias <= 0.0) return c1;
113 if (bias >= 1.0) return c2;
114 if (isnan(bias)) return c1;
115
116 qreal r = mixQreal(c1.redF(), c2.redF(), bias);
117 qreal g = mixQreal(c1.greenF(), c2.greenF(), bias);
118 qreal b = mixQreal(c1.blueF(), c2.blueF(), bias);
119 qreal a = mixQreal(c1.alphaF(), c2.alphaF(), bias);
120
121 return QColor::fromRgbF(r, g, b, a);
122}
123
124QColor KColorUtils::overlayColors(const QColor &base, const QColor &paint,
125 QPainter::CompositionMode comp)
126{
127 // This isn't the fastest way, but should be "fast enough".
128 // It's also the only safe way to use QPainter::CompositionMode
129 QImage img(1, 1, QImage::Format_ARGB32_Premultiplied);
130 QPainter p(&img);
131 QColor start = base;
132 start.setAlpha(255); // opaque
133 p.fillRect(0, 0, 1, 1, start);
134 p.setCompositionMode(comp);
135 p.fillRect(0, 0, 1, 1, paint);
136 p.end();
137 return img.pixel(0, 0);
138}
139// kate: space-indent on; indent-width 4; replace-tabs on; auto-insert-doxygen on;
KColorSpaces::KHCY
Definition: kcolorspaces.h:35
KColorSpaces::KHCY::qColor
QColor qColor() const
Definition: kcolorspaces.cpp:100
KColorSpaces::KHCY::y
qreal y
Definition: kcolorspaces.h:40
KColorSpaces::KHCY::c
qreal c
Definition: kcolorspaces.h:40
KColorSpaces::KHCY::luma
static qreal luma(const QColor &)
Definition: kcolorspaces.cpp:162
kcolorspaces.h
tintHelper
static QColor tintHelper(const QColor &base, qreal baseLuma, const QColor &color, qreal amount)
Definition: kcolorutils.cpp:79
contrastRatioForLuma
static qreal contrastRatioForLuma(qreal y1, qreal y2)
Definition: kcolorutils.cpp:42
mixQreal
static qreal mixQreal(qreal a, qreal b, qreal bias)
Definition: kcolorutils.cpp:31
kcolorutils.h
KColorUtils::darken
QColor darken(const QColor &, qreal amount=0.5, qreal chromaGain=1.0)
Adjust the luma of a color by changing its distance from black.
Definition: kcolorutils.cpp:63
KColorUtils::overlayColors
QColor overlayColors(const QColor &base, const QColor &paint, QPainter::CompositionMode comp=QPainter::CompositionMode_SourceOver)
Blend two colors into a new color by painting the second color over the first using the specified com...
Definition: kcolorutils.cpp:124
KColorUtils::luma
qreal luma(const QColor &)
Calculate the luma of a color.
Definition: kcolorutils.cpp:37
KColorUtils::shade
QColor shade(const QColor &, qreal lumaAmount, qreal chromaAmount=0.0)
Adjust the luma and chroma components of a color.
Definition: kcolorutils.cpp:71
KColorUtils::mix
QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
Blend two colors into a new color by linear combination.
Definition: kcolorutils.cpp:110
KColorUtils::tint
QColor tint(const QColor &base, const QColor &color, qreal amount=0.3)
Create a new color by tinting one color with another.
Definition: kcolorutils.cpp:87
KColorUtils::lighten
QColor lighten(const QColor &, qreal amount=0.5, qreal chromaInverseGain=1.0)
Adjust the luma of a color by changing its distance from white.
Definition: kcolorutils.cpp:55
KColorUtils::contrastRatio
qreal contrastRatio(const QColor &, const QColor &)
Calculate the contrast ratio between two colors, according to the W3C/WCAG2.0 algorithm,...
Definition: kcolorutils.cpp:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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