File indexing completed on 2024-05-05 04:21:07

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_COLOR 0
0030 
0031 
0032 #include "kpColor.h"
0033 
0034 
0035 static inline int RoundUp2 (int val)
0036 {
0037     return val % 2 ? val + 1 : val;
0038 }
0039 
0040 static inline int Bound0_255 (int val)
0041 {
0042     return qBound (0, val, 255);
0043 }
0044 
0045 
0046 enum
0047 {
0048     BlendDark = 25,
0049     BlendNormal = 50,
0050     BlendLight = 75,
0051     BlendAdd = 100
0052 };
0053 
0054 // Adds the 2 given colors together and then multiplies by the given <percent>.
0055 static inline kpColor Blend (const kpColor &a, const kpColor &b,
0056         int percent = ::BlendNormal)
0057 {
0058     return kpColor (::Bound0_255 (::RoundUp2 (a.red () + b.red ()) * percent / 100),
0059                     ::Bound0_255 (::RoundUp2 (a.green () + b.green ()) * percent / 100),
0060                     ::Bound0_255 (::RoundUp2 (a.blue () + b.blue ()) * percent / 100));
0061 }
0062 
0063 static inline kpColor Add (const kpColor &a, const kpColor &b)
0064 {
0065     return ::Blend (a, b, ::BlendAdd);
0066 }
0067 
0068 
0069 // (intentionally _not_ an HSV darkener)
0070 static inline kpColor Dark (const kpColor &color)
0071 {
0072     return ::Blend (color, kpColor::Black);
0073 }
0074 
0075 
0076 // public static
0077 const int kpColor::Exact = 0;
0078 
0079 // public static
0080 const kpColor kpColor::Invalid;  // LOTODO: what's wrong with explicitly specifying () constructor?
0081 const kpColor kpColor::Transparent (0, 0, 0, true/*isTransparent*/);
0082 
0083 
0084 //
0085 // Make our own colors in case weird ones like "Qt::cyan"
0086 // (turquoise) get changed by Qt.
0087 //
0088 
0089 
0090 const kpColor kpColor::Red (255, 0, 0);
0091 const kpColor kpColor::Green (0, 255, 0);
0092 const kpColor kpColor::Blue (0, 0, 255);
0093 const kpColor kpColor::Black (0, 0, 0);
0094 const kpColor kpColor::White (255, 255, 255);
0095 
0096 const kpColor kpColor::Yellow = ::Add (kpColor::Red, kpColor::Green);
0097 const kpColor kpColor::Purple = ::Add (kpColor::Red, kpColor::Blue);
0098 const kpColor kpColor::Aqua = ::Add (kpColor::Green, kpColor::Blue);
0099 
0100 const kpColor kpColor::Gray = ::Blend (kpColor::Black, kpColor::White);
0101 const kpColor kpColor::LightGray = ::Blend (kpColor::Gray, kpColor::White);
0102 const kpColor kpColor::Orange = ::Blend (kpColor::Red, kpColor::Yellow);
0103 
0104 const kpColor kpColor::Pink = ::Blend (kpColor::Red, kpColor::White);
0105 const kpColor kpColor::LightGreen = ::Blend (kpColor::Green, kpColor::White);
0106 const kpColor kpColor::LightBlue = ::Blend (kpColor::Blue, kpColor::White);
0107 const kpColor kpColor::Tan = ::Blend (kpColor::Yellow, kpColor::White);
0108 
0109 const kpColor kpColor::DarkRed = ::Dark (kpColor::Red);
0110 const kpColor kpColor::DarkOrange = ::Dark (kpColor::Orange);
0111 const kpColor kpColor::Brown = kpColor::DarkOrange;
0112 const kpColor kpColor::DarkYellow = ::Dark (kpColor::Yellow);
0113 const kpColor kpColor::DarkGreen = ::Dark (kpColor::Green);
0114 const kpColor kpColor::DarkAqua = ::Dark (kpColor::Aqua);
0115 const kpColor kpColor::DarkBlue = ::Dark (kpColor::Blue);
0116 const kpColor kpColor::DarkPurple = ::Dark (kpColor::Purple);
0117 const kpColor kpColor::DarkGray = ::Dark (kpColor::Gray);