This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
ColorUtils.h
84 lines (72 loc) · 2.46 KB
/
ColorUtils.h
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
/* This class is an almagamation of several KDE color classes:
* KColorSpaces, KColorHelpers, KColorUtils
* Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
* Copyright (C) 2007 Olaf Schmidt <ojschmidt@kde.org>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2007 Zack Rusin <zack@kde.org>
* Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef COLORUTILS_H
#define COLORUTILS_H
#include <QtGui/QColor>
#include <math.h>
namespace ColorUtils {
///////////////////////////////////////////////////////////////////////////////
// HCY color space
// from kcolorspaces.cpp
#define HCY_REC 709 // use 709 for now
#if HCY_REC == 601
static const qreal yc[3] = { 0.299, 0.587, 0.114 };
#elif HCY_REC == 709
static const qreal yc[3] = {0.2126, 0.7152, 0.0722};
#else // use Qt values
static const qreal yc[3] = { 0.34375, 0.5, 0.15625 };
#endif
// from kcolorhelpers_p.h
inline qreal normalize(qreal a)
{
return (a < 1.0 ? (a > 0.0 ? a : 0.0) : 1.0);
}
// from kcolorspaces.cpp
inline qreal gamma(qreal n)
{
return pow(normalize(n), 2.2);
}
// from kcolorspaces.cpp
inline qreal lumag(qreal r, qreal g, qreal b)
{
return r*yc[0] + g*yc[1] + b*yc[2];
}
// from kcolorspaces.cpp
inline qreal luma(const QColor& color)
{
return lumag(gamma(color.redF()),
gamma(color.greenF()),
gamma(color.blueF()));
}
// from kcolorutils.cpp
inline qreal 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);
}
}
#endif