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-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.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 
38 struct Color
39 {
41  float r, g, b, a;
42 
44  explicit Color(float aR = 0.0f, float aG = 0.0f, float aB = 0.0f, float aA = 0.0f)
45  : r(aR), g(aG), b(aB), a(aA) {}
46 
47  inline Color Inverse() const
48  {
49  return Color(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a);
50  }
51 
53  inline float* Array()
54  {
55  return reinterpret_cast<float*>(this);
56  }
57 
59  inline const float* Array() const
60  {
61  return reinterpret_cast<const float*>(this);
62  }
63 
65  inline std::string ToString() const
66  {
67  std::stringstream s;
68  s.precision(3);
69  s << "(" << r << ", " << g << ", " << b << ", " << a << ")";
70  return s.str();
71  }
72 
73  inline bool operator==(const Color &other) const
74  {
75  return r == other.r && g == other.g && b == other.b && a == other.a;
76  }
77 
78  inline bool operator!=(const Color &other) const
79  {
80  return ! this->operator==(other);
81  }
82 
83  inline Color operator*(float scale) const
84  {
85  Color c = *this;
86  c.r *= scale;
87  c.g *= scale;
88  c.b *= scale;
89  c.a *= scale;
90  return c;
91  }
92 };
93 
100 struct IntColor
101 {
103  unsigned char r, g, b, a;
104 
106  explicit IntColor(unsigned char aR = 0, unsigned char aG = 0, unsigned char aB = 0, unsigned char aA = 0)
107  : r(aR), g(aG), b(aB), a(aA) {}
108 };
109 
110 inline Color IntColorToColor(IntColor color)
111 {
112  return Color(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
113 }
114 
115 inline IntColor ColorToIntColor(Color color)
116 {
117  return IntColor(static_cast<unsigned char>(color.r * 255.0f),
118  static_cast<unsigned char>(color.g * 255.0f),
119  static_cast<unsigned char>(color.b * 255.0f),
120  static_cast<unsigned char>(color.a * 255.0f));
121 }
122 
123 inline Color IntensityToColor(float intensity)
124 {
125  if (intensity <= 0.0f) return Color(0.0f, 0.0f, 0.0f, 0.0f);
126  if (intensity >= 1.0f) return Color(1.0f, 1.0f, 1.0f, 1.0f);
127 
128  return Color(intensity, intensity, intensity, intensity);
129 }
130 
135 struct ColorHSV
136 {
137  float h, s, v;
138 
139  ColorHSV(float aH = 0.0f, float aS = 0.0f, float aV = 0.0f)
140  : h(aH), s(aS), v(aV) {}
141 
143  inline std::string ToString() const
144  {
145  std::stringstream str;
146  str.precision(3);
147  str << "(" << h << ", " << s << ", " << v << ")";
148  return str.str();
149  }
150 };
151 
153 ColorHSV RGB2HSV(Color color);
154 
156 Color HSV2RGB(ColorHSV color);
157 
158 
159 } // namespace Gfx
160 
float r
Red, green, blue and alpha components.
Definition: color.h:41
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:44
Color HSV2RGB(ColorHSV color)
Converts a HSV color to RGB color.
Definition: color.cpp:70
std::string ToString() const
Returns a string "(h, s, v)".
Definition: color.h:143
std::string ToString() const
Returns a string (r, g, b, a)
Definition: color.h:65
Color with integer values.
Definition: color.h:100
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:106
ColorHSV RGB2HSV(Color color)
Converts a RGB color to HSV color.
Definition: color.cpp:30
Namespace for (new) graphics code.
Definition: app.h:49
float * Array()
Returns the struct cast to float* array; use with care!
Definition: color.h:53
RGBA color.
Definition: color.h:38
HSV color.
Definition: color.h:135
unsigned char r
Red, green, blue and alpha components.
Definition: color.h:103
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: color.h:59