Colobot
color.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include <sstream>
29 
30 
31 // Graphics module namespace
32 namespace Gfx
33 {
34 
39 struct Color
40 {
42  float r, g, b, a;
43 
45  explicit Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f)
46  : r(aR), g(aG), b(aB), a(aA) {}
47 
48  inline Color Inverse() const
49  {
50  return Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a);
51  }
52 
54  inline float* Array()
55  {
56  return reinterpret_cast<float*>(this);
57  }
58 
60  inline const float* Array() const
61  {
62  return reinterpret_cast<const float*>(this);
63  }
64 
66  inline std::string ToString() const
67  {
68  std::stringstream s;
69  s.precision(3);
70  s << "(" << r << ", " << g << ", " << b << ", " << a << ")";
71  return s.str();
72  }
73 
74  inline bool operator==(const Color &other) const
75  {
76  return r == other.r && g == other.g && b == other.b && a == other.a;
77  }
78 
79  inline bool operator!=(const Color &other) const
80  {
81  return ! this->operator==(other);
82  }
83 
84  inline Color operator*(float scale) const
85  {
86  Color c = *this;
87  c.r *= scale;
88  c.g *= scale;
89  c.b *= scale;
90  c.a *= scale;
91  return c;
92  }
93 };
94 
101 struct IntColor
102 {
104  unsigned char r, g, b, a;
105 
107  explicit IntColor(unsigned char aR = 0, unsigned char aG = 0, unsigned char aB = 0, unsigned char aA = 0)
108  : r(aR), g(aG), b(aB), a(aA) {}
109 };
110 
111 inline Color IntColorToColor(IntColor color)
112 {
113  return Color(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
114 }
115 
116 inline IntColor ColorToIntColor(Color color)
117 {
118  return IntColor(static_cast<unsigned char>(color.r * 255.0f),
119  static_cast<unsigned char>(color.g * 255.0f),
120  static_cast<unsigned char>(color.b * 255.0f),
121  static_cast<unsigned char>(color.a * 255.0f));
122 }
123 
124 inline Color IntensityToColor(float intensity)
125 {
126  if (intensity <= 0.0f) return Color(0.0f, 0.0f, 0.0f, 0.0f);
127  if (intensity >= 1.0f) return Color(1.0f, 1.0f, 1.0f, 1.0f);
128 
129  return Color(intensity, intensity, intensity, intensity);
130 }
131 
136 struct ColorHSV
137 {
138  float h, s, v;
139 
140  ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f)
141  : h(aH), s(aS), v(aV) {}
142 
144  inline std::string ToString() const
145  {
146  std::stringstream str;
147  str.precision(3);
148  str << "(" << h << ", " << s << ", " << v << ")";
149  return str.str();
150  }
151 };
152 
154 ColorHSV RGB2HSV(Color color);
155 
157 Color HSV2RGB(ColorHSV color);
158 
159 
160 } // namespace Gfx
161 
float r
Red, green, blue and alpha components.
Definition: color.h:42
Color(float aR=0.0f, float aG=0.0f, float aB=0.0f, float aA=0.0f)
Constructor; default values are (0,0,0,0) = black.
Definition: color.h:45
Color HSV2RGB(ColorHSV color)
Converts a HSV color to RGB color.
Definition: color.cpp:71
std::string ToString() const
Returns a string "(h, s, v)".
Definition: color.h:144
std::string ToString() const
Returns a string (r, g, b, a)
Definition: color.h:66
Color with integer values.
Definition: color.h:101
IntColor(unsigned char aR=0, unsigned char aG=0, unsigned char aB=0, unsigned char aA=0)
Constructor; default values are (0,0,0,0) = black.
Definition: color.h:107
ColorHSV RGB2HSV(Color color)
Converts a RGB color to HSV color.
Definition: color.cpp:31
Namespace for (new) graphics code.
Definition: app.h:49
float * Array()
Returns the struct cast to float* array; use with care!
Definition: color.h:54
RGBA color.
Definition: color.h:39
HSV color.
Definition: color.h:136
unsigned char r
Red, green, blue and alpha components.
Definition: color.h:104
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: color.h:60