summaryrefslogtreecommitdiffstats
path: root/lib/kopainter/koColor.h
blob: 70fffd1fa063de457b7042bb69a62195c4bc420c (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* This file is part of the KDE project
  Copyright (c) 1999 Matthias Elter (me@kde.org)
  Copyright (c) 2001-2002 Igor Jansen (rm@kde.org)

   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 __ko_color_h__
#define __ko_color_h__

#include <tqcolor.h>
#include <koffice_export.h>
class KOPAINTER_EXPORT KoColor
{
public:
  enum cSpace{ csIndexed, csRGB, csHSV, csCMYK, csLab };

  KoColor();
  KoColor(int a, int b, int c,  cSpace m = csRGB);
  KoColor(int c, int m, int y,  int k);
  KoColor(const TQString &name);
  KoColor(const TQColor &c);

  cSpace native() const {return mNative; }

  int R() const;
  int G() const;
  int B() const;
  int H() const;
  int S() const;
  int V() const;
  int L() const;
  int a() const;
  int b() const;
  int C() const;
  int M() const;
  int Y() const;
  int K() const;

  void rgb(int *R, int *G, int *B) const;
  void hsv(int *H, int *S, int *V) const;
  void lab(int *L, int *a, int *b) const;
  void cmyk(int *C, int *M, int *Y, int *K) const;
  TQString name() const;
  TQColor color() const;

  void setRGB(int R, int G, int B);
  void setHSV(int H, int S, int V);
  void setLab(int L, int a, int b);
  void setCMYK(int C, int M, int Y, int K);
  void setNamedColor(const TQString &name);
  void setColor(const TQColor &c);
  
  static void RGBtoHSV(int R, int G, int B, int *H, int *S, int *V);
  static void RGBtoLAB(int R, int G, int B, int *L, int *a, int *b);
  static void RGBtoCMYK(int R, int G, int B, int *C, int *M, int *Y, int *K);

  static void HSVtoRGB(int H, int S, int V, int *R, int *G, int *B);
  static void HSVtoLAB(int H, int S, int V, int *L, int *a, int *b);
  static void HSVtoCMYK(int H, int S, int V, int *C, int *M, int *Y, int*K);

  static void LABtoRGB(int L, int a, int b, int *R, int *G, int *B);
  static void LABtoHSV(int L, int a, int b, int *H, int *S, int *V);
  static void LABtoCMYK(int L, int a, int b, int *C, int *M, int *Y, int*K);

  static void CMYKtoRGB(int C, int M, int Y, int K, int *R, int *G, int *B);
  static void CMYKtoHSV(int C, int M, int Y, int K, int *H, int *S, int *V);
  static void CMYKtoLAB(int C, int M, int Y, int K, int *L, int *a, int *b);

  static const KoColor black();
  static const KoColor white();
  static const KoColor gray();
  static const KoColor lightGray();
  static const KoColor darkGray();
  static const KoColor red();
  static const KoColor darkRed();
  static const KoColor green();
  static const KoColor darkGreen();
  static const KoColor blue();
  static const KoColor darkBlue();
  static const KoColor cyan();
  static const KoColor darkCyan();
  static const KoColor magenta();
  static const KoColor darkMagenta();
  static const KoColor yellow();
  static const KoColor darkYellow();

protected:
  int hex2int(TQChar c);

  void calcRGB() const;
  void calcHSV() const;
  void calcCMYK() const;
  void calcLAB() const;

  void rgbChanged() const;
  void hsvChanged() const;
  void cmykChanged() const;
  void labChanged() const;

private:
  /*
   * Mutable to make it possible for const objects to transform the native cModel
   * in functions like KoColor::rgb(...) to the requested.
   */
  mutable int mR, mG, mB;        // RGB
  mutable int mC, mM, mY, mK;    // CMYK
  mutable int mH, mS, mV;        // HSV
  mutable int mL, ma, mb;        // LAB

  mutable bool mRGBvalid;
  mutable bool mHSVvalid;
  mutable bool mCMYKvalid;
  mutable bool mLABvalid;

  cSpace mNative; 
};

inline const KoColor KoColor::white()
{
  return KoColor(255, 255, 255, csRGB);
}

inline const KoColor KoColor::black()
{
  return KoColor(0, 0, 0, csRGB);
}

inline const KoColor KoColor::gray()
{
  return KoColor(160, 160, 164, csRGB);
}

inline const KoColor KoColor::lightGray()
{
  return KoColor(192, 192, 192, csRGB);
}

inline const KoColor KoColor::darkGray()
{
  return KoColor(128, 128, 128, csRGB);
}

inline const KoColor KoColor::red()
{
  return KoColor(255, 0, 0, csRGB);
}

inline const KoColor KoColor::darkRed()
{
  return KoColor(128, 0, 0, csRGB);
}

inline const KoColor KoColor::green()
{
  return KoColor(0, 255, 0, csRGB);
}

inline const KoColor KoColor::darkGreen()
{
  return KoColor(0, 128, 0, csRGB);
}

inline const KoColor KoColor::blue()
{
  return KoColor(0, 0, 255, csRGB);
}

inline const KoColor KoColor::darkBlue()
{
  return KoColor(0, 0, 128, csRGB);
}

inline const KoColor KoColor::cyan()
{
  return KoColor(0, 255, 255, csRGB);
}

inline const KoColor KoColor::darkCyan()
{
  return KoColor(0, 128, 128, csRGB);
}

inline const KoColor KoColor::magenta()
{
  return KoColor(255, 0, 255, csRGB);
}

inline const KoColor KoColor::darkMagenta()
{
  return KoColor(128, 0, 128, csRGB);
}

inline const KoColor KoColor::yellow()
{
  return KoColor(255, 255, 0, csRGB);
}

inline const KoColor KoColor::darkYellow()
{
  return KoColor(128, 128, 0, csRGB);
}

#endif