Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
color.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
3 // *
4 // * This program is free software: you can redistribute it and/or modify
5 // * it under the terms of the GNU General Public License as published by
6 // * the Free Software Foundation, either version 3 of the License, or
7 // * (at your option) any later version.
8 // *
9 // * This program is distributed in the hope that it will be useful,
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // * GNU General Public License for more details.
13 // *
14 // * You should have received a copy of the GNU General Public License
15 // * along with this program. If not, see http://www.gnu.org/licenses/.
16 
22 #pragma once
23 
24 
25 #include <sstream>
26 
27 
28 // Graphics module namespace
29 namespace Gfx {
30 
35 struct Color
36 {
38  float r, g, b, a;
39 
41  explicit Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f)
42  : r(aR), g(aG), b(aB), a(aA) {}
43 
44  inline Color Inverse() const
45  {
46  return Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a);
47  }
48 
50  inline float* Array()
51  {
52  return reinterpret_cast<float*>(this);
53  }
54 
56  inline const float* Array() const
57  {
58  return reinterpret_cast<const float*>(this);
59  }
60 
62  inline std::string ToString() const
63  {
64  std::stringstream s;
65  s.precision(3);
66  s << "(" << r << ", " << g << ", " << b << ", " << a << ")";
67  return s.str();
68  }
69 
70  inline bool operator==(const Color &other) const
71  {
72  return r == other.r && g == other.g && b == other.b && a == other.a;
73  }
74 
75  inline bool operator!=(const Color &other) const
76  {
77  return ! this->operator==(other);
78  }
79 
80  inline Color operator*(float scale) const
81  {
82  Color c = *this;
83  c.r *= scale;
84  c.g *= scale;
85  c.b *= scale;
86  c.a *= scale;
87  return c;
88  }
89 };
90 
97 struct IntColor
98 {
100  unsigned char r, g, b, a;
101 
103  explicit IntColor(unsigned char aR = 0, unsigned char aG = 0, unsigned char aB = 0, unsigned char aA = 0)
104  : r(aR), g(aG), b(aB), a(aA) {}
105 };
106 
107 inline Color IntColorToColor(IntColor color)
108 {
109  return Color(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
110 }
111 
112 inline IntColor ColorToIntColor(Color color)
113 {
114  return IntColor(static_cast<unsigned char>(color.r * 255.0f),
115  static_cast<unsigned char>(color.g * 255.0f),
116  static_cast<unsigned char>(color.b * 255.0f),
117  static_cast<unsigned char>(color.a * 255.0f));
118 }
119 
120 inline Color IntensityToColor(float intensity)
121 {
122  if (intensity <= 0.0f) return Color(0.0f, 0.0f, 0.0f, 0.0f);
123  if (intensity >= 1.0f) return Color(1.0f, 1.0f, 1.0f, 1.0f);
124 
125  return Color(intensity, intensity, intensity, intensity);
126 }
127 
132 struct ColorHSV
133 {
134  float h, s, v;
135 
136  ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f)
137  : h(aH), s(aS), v(aV) {}
138 
140  inline std::string ToString() const
141  {
142  std::stringstream str;
143  str.precision(3);
144  str << "(" << h << ", " << s << ", " << v << ")";
145  return str.str();
146  }
147 };
148 
150 ColorHSV RGB2HSV(Color color);
151 
153 Color HSV2RGB(ColorHSV color);
154 
155 
156 } // namespace Gfx
157