hx3d  1
2D/3D Simple Game Framework
color.cpp
1 /*
2  Color.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/color.hpp"
22 
23 namespace hx3d {
24 namespace graphics {
25 
27  r(255), g(255), b(255), a(255) {}
28 Color::Color(unsigned char r, unsigned char g, unsigned char b):
29  r(r), g(g), b(b), a(255) {}
30 Color::Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a):
31  r(r), g(g), b(b), a(a) {}
32 
33 Color& Color::operator=(const Color& color) {
34  r = color.r;
35  g = color.g;
36  b = color.b;
37  a = color.a;
38 
39  return *this;
40 }
41 
42 glm::vec4 Color::toFloat() {
43  return glm::vec4(r/255.f, g/255.f, b/255.f, a/255.f);
44 }
45 
47 {
48  Color rgb;
49  unsigned char region, remainder, p, q, t;
50 
51  if (hsv.g == 0)
52  {
53  rgb.r = hsv.r;
54  rgb.g = hsv.g;
55  rgb.b = hsv.b;
56  return rgb;
57  }
58 
59  region = hsv.r / 43;
60  remainder = (hsv.r - (region * 43)) * 6;
61 
62  p = (hsv.b * (255 - hsv.g)) >> 8;
63  q = (hsv.b * (255 - ((hsv.g * remainder) >> 8))) >> 8;
64  t = (hsv.b * (255 - ((hsv.g * (255 - remainder)) >> 8))) >> 8;
65 
66  switch (region)
67  {
68  case 0:
69  rgb.r = hsv.b; rgb.g = t; rgb.b = p;
70  break;
71  case 1:
72  rgb.r = q; rgb.g = hsv.b; rgb.b = p;
73  break;
74  case 2:
75  rgb.r = p; rgb.g = hsv.b; rgb.b = t;
76  break;
77  case 3:
78  rgb.r = p; rgb.g = q; rgb.b = hsv.b;
79  break;
80  case 4:
81  rgb.r = t; rgb.g = p; rgb.b = hsv.b;
82  break;
83  default:
84  rgb.r = hsv.b; rgb.g = p; rgb.b = q;
85  break;
86  }
87 
88  return rgb;
89 }
90 
92 {
93  Color hsv;
94  unsigned char rgbMin, rgbMax;
95 
96  rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
97  rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
98 
99  hsv.b = rgbMax;
100  if (hsv.b == 0)
101  {
102  hsv.r = 0;
103  hsv.g = 0;
104  return hsv;
105  }
106 
107  hsv.g = 255 * long(rgbMax - rgbMin) / hsv.b;
108  if (hsv.g == 0)
109  {
110  hsv.r = 0;
111  return hsv;
112  }
113 
114  if (rgbMax == rgb.r)
115  hsv.r = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
116  else if (rgbMax == rgb.g)
117  hsv.r = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
118  else
119  hsv.r = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);
120 
121  return hsv;
122 }
123 
124 Color Color::White = Color(255, 255, 255);
125 Color Color::Black = Color(0, 0, 0);
126 Color Color::Red = Color(255, 0, 0);
127 Color Color::Green = Color(0, 255, 0);
128 Color Color::Blue = Color(0, 0, 255);
129 
130 } /* graphics */
131 } /* hx3d */
glm::vec4 toFloat()
Convert the color to a float format (between 0 and 1).
Definition: color.cpp:42
unsigned char a
Alpha component.
Definition: color.hpp:102
unsigned char g
Green component.
Definition: color.hpp:98
static Color hsvToRgb(Color hsv)
Convert HSV color to RGB format.
Definition: color.cpp:46
Four [0..255] components defined color.
Definition: color.hpp:32
static Color Red
Red color.
Definition: color.hpp:111
static Color White
White color.
Definition: color.hpp:107
hx3d framework namespace
Definition: audio.hpp:26
Color()
Create a white color.
Definition: color.cpp:26
static Color Blue
Blue color.
Definition: color.hpp:115
Color & operator=(const Color &color)
Affect a color into another.
Definition: color.cpp:33
static Color rgbToHsv(Color rgb)
Convert RGB color to HSV format.
Definition: color.cpp:91
static Color Green
Green color.
Definition: color.hpp:113
static Color Black
Black color.
Definition: color.hpp:109
unsigned char b
Blue component.
Definition: color.hpp:100
unsigned char r
Red component.
Definition: color.hpp:96