1 
2 /*
3    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4    All rights reserved.
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
10    1. Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12    2. Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15 
16    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 
29 #define DEBUG_KP_COLOR 0
30 
31 
32 #include "kpColor.h"
33 
34 
RoundUp2(int val)35 static inline int RoundUp2 (int val)
36 {
37     return val % 2 ? val + 1 : val;
38 }
39 
Bound0_255(int val)40 static inline int Bound0_255 (int val)
41 {
42     return qBound (0, val, 255);
43 }
44 
45 
46 enum
47 {
48     BlendDark = 25,
49     BlendNormal = 50,
50     BlendLight = 75,
51     BlendAdd = 100
52 };
53 
54 // Adds the 2 given colors together and then multiplies by the given <percent>.
Blend(const kpColor & a,const kpColor & b,int percent=::BlendNormal)55 static inline kpColor Blend (const kpColor &a, const kpColor &b,
56         int percent = ::BlendNormal)
57 {
58     return kpColor (::Bound0_255 (::RoundUp2 (a.red () + b.red ()) * percent / 100),
59                     ::Bound0_255 (::RoundUp2 (a.green () + b.green ()) * percent / 100),
60                     ::Bound0_255 (::RoundUp2 (a.blue () + b.blue ()) * percent / 100));
61 }
62 
Add(const kpColor & a,const kpColor & b)63 static inline kpColor Add (const kpColor &a, const kpColor &b)
64 {
65     return ::Blend (a, b, ::BlendAdd);
66 }
67 
68 
69 // (intentionally _not_ an HSV darkener)
Dark(const kpColor & color)70 static inline kpColor Dark (const kpColor &color)
71 {
72     return ::Blend (color, kpColor::Black);
73 }
74 
75 
76 // public static
77 const int kpColor::Exact = 0;
78 
79 // public static
80 const kpColor kpColor::Invalid;  // LOTODO: what's wrong with explicitly specifying () constructor?
81 const kpColor kpColor::Transparent (0, 0, 0, true/*isTransparent*/);
82 
83 
84 //
85 // Make our own colors in case weird ones like "Qt::cyan"
86 // (turquoise) get changed by Qt.
87 //
88 
89 
90 const kpColor kpColor::Red (255, 0, 0);
91 const kpColor kpColor::Green (0, 255, 0);
92 const kpColor kpColor::Blue (0, 0, 255);
93 const kpColor kpColor::Black (0, 0, 0);
94 const kpColor kpColor::White (255, 255, 255);
95 
96 const kpColor kpColor::Yellow = ::Add (kpColor::Red, kpColor::Green);
97 const kpColor kpColor::Purple = ::Add (kpColor::Red, kpColor::Blue);
98 const kpColor kpColor::Aqua = ::Add (kpColor::Green, kpColor::Blue);
99 
100 const kpColor kpColor::Gray = ::Blend (kpColor::Black, kpColor::White);
101 const kpColor kpColor::LightGray = ::Blend (kpColor::Gray, kpColor::White);
102 const kpColor kpColor::Orange = ::Blend (kpColor::Red, kpColor::Yellow);
103 
104 const kpColor kpColor::Pink = ::Blend (kpColor::Red, kpColor::White);
105 const kpColor kpColor::LightGreen = ::Blend (kpColor::Green, kpColor::White);
106 const kpColor kpColor::LightBlue = ::Blend (kpColor::Blue, kpColor::White);
107 const kpColor kpColor::Tan = ::Blend (kpColor::Yellow, kpColor::White);
108 
109 const kpColor kpColor::DarkRed = ::Dark (kpColor::Red);
110 const kpColor kpColor::DarkOrange = ::Dark (kpColor::Orange);
111 const kpColor kpColor::Brown = kpColor::DarkOrange;
112 const kpColor kpColor::DarkYellow = ::Dark (kpColor::Yellow);
113 const kpColor kpColor::DarkGreen = ::Dark (kpColor::Green);
114 const kpColor kpColor::DarkAqua = ::Dark (kpColor::Aqua);
115 const kpColor kpColor::DarkBlue = ::Dark (kpColor::Blue);
116 const kpColor kpColor::DarkPurple = ::Dark (kpColor::Purple);
117 const kpColor kpColor::DarkGray = ::Dark (kpColor::Gray);
118