1 // color.h
2 //
3 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9
10 #ifndef _CELUTIL_COLOR_H_
11 #define _CELUTIL_COLOR_H_
12
13 #include <map>
14 #include <string>
15
16
17 class Color
18 {
19 public:
20 Color();
21 Color(float, float, float);
22 Color(float, float, float, float);
23 Color(unsigned char, unsigned char, unsigned char);
24 Color(const Color&, float);
25
26 enum {
27 Red = 0,
28 Green = 1,
29 Blue = 2,
30 Alpha = 3
31 };
32
33 inline float red() const;
34 inline float green() const;
35 inline float blue() const;
36 inline float alpha() const;
37 inline void get(unsigned char*) const;
38
39 friend bool operator==(Color, Color);
40 friend bool operator!=(Color, Color);
41 friend Color operator*(Color, Color);
42
43 static const Color Black;
44 static const Color White;
45
46 static bool parse(const char*, Color&);
47
48 private:
49 static void buildX11ColorMap();
50
51 private:
52 unsigned char c[4];
53
54 typedef std::map<const std::string, Color> ColorMap;
55 static ColorMap x11Colors;
56 };
57
58
red()59 float Color::red() const
60 {
61 return c[Red] * (1.0f / 255.0f);
62 }
63
green()64 float Color::green() const
65 {
66 return c[Green] * (1.0f / 255.0f);
67 }
68
blue()69 float Color::blue() const
70 {
71 return c[Blue] * (1.0f / 255.0f);
72 }
73
alpha()74 float Color::alpha() const
75 {
76 return c[Alpha] * (1.0f / 255.0f);
77 }
78
get(unsigned char * rgba)79 void Color::get(unsigned char* rgba) const
80 {
81 rgba[0] = c[Red];
82 rgba[1] = c[Green];
83 rgba[2] = c[Blue];
84 rgba[3] = c[Alpha];
85 }
86
87 inline bool operator==(Color a, Color b)
88 {
89 return (a.c[0] == b.c[2] && a.c[1] == b.c[1] &&
90 a.c[2] == b.c[2] && a.c[3] == b.c[3]);
91 }
92
93 inline bool operator!=(Color a, Color b)
94 {
95 return !(a == b);
96 }
97
98 inline Color operator*(Color a, Color b)
99 {
100 return Color(a.red() * b.red(),
101 a.green() * b.green(),
102 a.blue() * b.blue(),
103 a.alpha() * b.alpha());
104 }
105
106 #endif // _CELUTIL_COLOR_H_
107