aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/color
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2014-04-08 05:17:27 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2014-04-08 05:17:27 +0200
commitac164afa0954e28b521dc6b19d2ccf0320cc8ed1 (patch)
tree576420091adcad7b91dc1c8b296cc36b3d827b74 /src/utils/color
parent196f41688d01b7f0a79567c617fd6dd3171150ff (diff)
downloadusdx-ac164afa0954e28b521dc6b19d2ccf0320cc8ed1.tar.gz
usdx-ac164afa0954e28b521dc6b19d2ccf0320cc8ed1.tar.xz
usdx-ac164afa0954e28b521dc6b19d2ccf0320cc8ed1.zip
utils/colors: move colors into subdir, add hsv
All color classes are now in an own subdirectory with a short name. The new class HsvColor represents the Hue/Saturation/Value color model and comes with conversion functions to and from rgb colors (currently integer arithmetic without optimization for higher presicion).
Diffstat (limited to 'src/utils/color')
-rw-r--r--src/utils/color/hsv.cpp105
-rw-r--r--src/utils/color/hsv.hpp66
-rw-r--r--src/utils/color/rgb.cpp171
-rw-r--r--src/utils/color/rgb.hpp67
-rw-r--r--src/utils/color/rgba.cpp99
-rw-r--r--src/utils/color/rgba.hpp51
6 files changed, 559 insertions, 0 deletions
diff --git a/src/utils/color/hsv.cpp b/src/utils/color/hsv.cpp
new file mode 100644
index 00000000..11df9ec2
--- /dev/null
+++ b/src/utils/color/hsv.cpp
@@ -0,0 +1,105 @@
+/*
+ * UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "hsv.hpp"
+#include "rgb.hpp"
+
+namespace usdx
+{
+ HsvColor::HsvColor(uint8_t hue, uint8_t saturation, uint8_t value)
+ : hue(hue), saturation(saturation), value(value)
+ {
+ }
+
+ HsvColor::HsvColor(const HsvColor& color)
+ : hue(color.hue), saturation(color.saturation), value(color.value)
+ {
+ }
+
+ HsvColor::HsvColor(const RgbColor& rgb)
+ : hue(0), saturation(0), value(0)
+ {
+ // find min and max RGB value
+ uint8_t rgb_min = rgb.get_red();
+ if (rgb.get_green() < rgb_min) rgb_min = rgb.get_green();
+ if (rgb.get_blue() < rgb_min) rgb_min = rgb.get_blue();
+
+ uint8_t rgb_max = rgb.get_red();
+ if (rgb.get_green() > rgb_max) rgb_max = rgb.get_green();
+ if (rgb.get_blue() > rgb_max) rgb_max = rgb.get_blue();
+
+ uint8_t rgb_delta = rgb_max - rgb_min;
+
+ if (rgb_max != 0) {
+ // compute value
+ value = rgb_max;
+
+ if (rgb_delta != 0) {
+ // compute saturation
+ saturation = (255 * (rgb_max - rgb_min)) / value;
+
+ // compute hue
+ if (rgb_max == rgb.get_red()) {
+ hue = 0 + 43 * (rgb.get_green() - rgb.get_blue()) / rgb_delta;
+ } else if (rgb_max == rgb.get_green()) {
+ hue = 85 + 43 * (rgb.get_blue() - rgb.get_red()) / rgb_delta;
+ } else /* rgb_max == rgb.get_blue() */ {
+ hue = 171 + 43 * (rgb.get_red() - rgb.get_green()) / rgb_delta;
+ }
+ }
+ }
+ }
+
+ HsvColor::~HsvColor()
+ {
+ }
+
+ HsvColor& HsvColor::operator=(const HsvColor& color)
+ {
+ if (this != &color) {
+ hue = color.hue;
+ saturation = color.saturation;
+ value = color.value;
+ }
+
+ return *this;
+
+ }
+
+ uint8_t HsvColor::get_hue(void) const
+ {
+ return hue;
+ }
+
+ uint8_t HsvColor::get_saturation(void) const
+ {
+ return saturation;
+ }
+
+ uint8_t HsvColor::get_value(void) const
+ {
+ return value;
+ }
+
+};
diff --git a/src/utils/color/hsv.hpp b/src/utils/color/hsv.hpp
new file mode 100644
index 00000000..748a9b74
--- /dev/null
+++ b/src/utils/color/hsv.hpp
@@ -0,0 +1,66 @@
+/*
+ * UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef HSV_HPP
+#define HSV_HPP
+
+#include <stdint.h>
+
+namespace usdx
+{
+ class RgbColor;
+
+ class HsvColor
+ {
+ private:
+ /**
+ * Hue degree between 0 and 255
+ */
+ uint8_t hue;
+
+ /**
+ * Saturation between 0 (gray) and 255
+ */
+ uint8_t saturation;
+
+ /**
+ * Value between 0 (black) and 255
+ */
+ uint8_t value;
+
+ public:
+ HsvColor(uint8_t hue, uint8_t saturation, uint8_t value);
+ HsvColor(const HsvColor& color);
+ HsvColor(const RgbColor& rgb);
+ virtual ~HsvColor();
+ HsvColor& operator=(const HsvColor& color);
+
+ uint8_t get_hue(void) const;
+ uint8_t get_saturation(void) const;
+ uint8_t get_value(void) const;
+ };
+
+};
+
+#endif
diff --git a/src/utils/color/rgb.cpp b/src/utils/color/rgb.cpp
new file mode 100644
index 00000000..c43b85c4
--- /dev/null
+++ b/src/utils/color/rgb.cpp
@@ -0,0 +1,171 @@
+/*
+ * UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include <cstddef>
+
+#include "rgb.hpp"
+#include "hsv.hpp"
+
+namespace usdx
+{
+ RgbColor::RgbColor(uint8_t red, uint8_t green, uint8_t blue)
+ : red(red), green(green), blue(blue),
+ array(NULL), array_length(0)
+ {
+ }
+
+ RgbColor::RgbColor(const uint32_t color)
+ : red((color & 0xff0000) >> 16), green((color & 0xff00) >> 8),
+ blue(color & 0xff), array(NULL), array_length(0)
+ {
+ }
+
+ RgbColor::RgbColor(const RgbColor& color)
+ : red(color.red), green(color.green), blue(color.blue), array(NULL),
+ array_length(0)
+ {
+ }
+
+ RgbColor::RgbColor(const HsvColor& hsv)
+ : red(0), green(0), blue(0), array(NULL), array_length(0)
+ {
+ uint8_t region, fpart, p, q, t;
+
+ if(hsv.get_saturation() == 0) {
+ /* color is grayscale */
+ red = green = blue = hsv.get_value();
+ return;
+ }
+
+ /* make hue 0-5 */
+ region = hsv.get_value() / 43;
+
+ /* find remainder part, make it from 0-255 */
+ fpart = (hsv.get_value() - (region * 43)) * 6;
+
+ /* calculate temp vars, doing integer multiplication */
+ p = (hsv.get_value() * (255 - hsv.get_saturation())) >> 8;
+ q = (hsv.get_value() * (255 - ((hsv.get_saturation() * fpart) >> 8))) >> 8;
+ t = (hsv.get_value() * (255 - ((hsv.get_saturation() * (255 - fpart)) >> 8))) >> 8;
+
+ /* assign temp vars based on color cone region */
+ switch(region) {
+ case 0:
+ red = hsv.get_value(); green = t; blue = p; break;
+ case 1:
+ red = q; green = hsv.get_value(); blue = p; break;
+ case 2:
+ red = p; green = hsv.get_value(); blue = t; break;
+ case 3:
+ red = p; green = q; blue = hsv.get_value(); break;
+ case 4:
+ red = t; green = p; blue = hsv.get_value(); break;
+ default:
+ red = hsv.get_value(); green = p; blue = q; break;
+ }
+ }
+
+ RgbColor::~RgbColor()
+ {
+ if (array != NULL) {
+ delete[] array;
+ array = NULL;
+ }
+ }
+
+ RgbColor& RgbColor::operator=(const RgbColor& color)
+ {
+ if (this != &color) {
+ red = color.red;
+ green = color.green;
+ blue = color.blue;
+ array_length = 0;
+
+ if (array != NULL) {
+ delete[] array;
+ array = NULL;
+ }
+ }
+
+ return *this;
+ }
+
+ uint8_t RgbColor::get_red(void) const
+ {
+ return red;
+ }
+
+ uint8_t RgbColor::get_green(void) const
+ {
+ return green;
+ }
+
+ uint8_t RgbColor::get_blue(void) const
+ {
+ return blue;
+ }
+
+ const GLubyte* RgbColor::get_array(size_t len) const
+ {
+ if (array_length != len && array != NULL) {
+ delete[] array;
+ array = NULL;
+ array_length = 0;
+ }
+
+ if (array == NULL) {
+ int components = get_array_comonent_count();
+ array = new GLubyte[len * components];
+ array_length = len;
+
+ for (size_t i = 0; i < len; i++) {
+ array[i*components] = red;
+ array[(i*components) + 1] = green;
+ array[(i*components) + 2] = blue;
+ }
+ }
+
+ return array;
+ }
+
+ int RgbColor::get_array_comonent_count(void) const
+ {
+ return 3;
+ }
+
+ bool RgbColor::is_grey(void) const
+ {
+ return (red == green) && (green == blue);
+ }
+
+ bool RgbColor::is_black(void) const
+ {
+ return (red == 0) && (green == 0) && (blue == 0);
+ }
+
+ bool RgbColor::is_white(void) const
+ {
+ return (red == 255) && (green == 255) && (blue == 255);
+ }
+};
diff --git a/src/utils/color/rgb.hpp b/src/utils/color/rgb.hpp
new file mode 100644
index 00000000..6871e452
--- /dev/null
+++ b/src/utils/color/rgb.hpp
@@ -0,0 +1,67 @@
+/*
+ * UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef RGB_COLOR_HPP
+#define RGB_COLOR_HPP
+
+#include <GL/gl.h>
+#include <stdint.h>
+
+namespace usdx
+{
+ class HsvColor;
+
+ class RgbColor
+ {
+ private:
+ uint8_t red;
+ uint8_t green;
+ uint8_t blue;
+
+ protected:
+ mutable GLubyte *array;
+ mutable size_t array_length;
+
+ public:
+ RgbColor(uint8_t red, uint8_t green, uint8_t blue);
+ RgbColor(const uint32_t color);
+ RgbColor(const RgbColor& color);
+ RgbColor(const HsvColor& hsv);
+ virtual ~RgbColor();
+ RgbColor& operator=(const RgbColor& color);
+
+ uint8_t get_red(void) const;
+ uint8_t get_green(void) const;
+ uint8_t get_blue(void) const;
+
+ virtual const GLubyte* get_array(size_t len) const;
+ int get_array_comonent_count(void) const;
+
+ bool is_grey(void) const;
+ bool is_black(void) const;
+ bool is_white(void) const;
+ };
+};
+
+#endif
diff --git a/src/utils/color/rgba.cpp b/src/utils/color/rgba.cpp
new file mode 100644
index 00000000..777bb720
--- /dev/null
+++ b/src/utils/color/rgba.cpp
@@ -0,0 +1,99 @@
+/*
+ * UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include <cstddef>
+
+#include "rgba.hpp"
+
+namespace usdx
+{
+ RgbaColor::RgbaColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
+ : RgbColor(red, green, blue), alpha(alpha)
+ {
+ }
+
+ RgbaColor::RgbaColor(const RgbaColor& color)
+ : RgbColor(color), alpha(color.alpha)
+ {
+ }
+
+ RgbaColor::RgbaColor(const RgbColor& color, uint8_t alpha)
+ : RgbColor(color), alpha(alpha)
+ {
+ }
+
+ RgbaColor& RgbaColor::operator=(const RgbaColor& color)
+ {
+ if (this != &color) {
+ RgbColor::operator=(color);
+ alpha = color.alpha;
+ }
+
+ return *this;
+ }
+
+ RgbaColor& RgbaColor::operator=(const RgbColor& color)
+ {
+ if (this != &color) {
+ RgbColor::operator=(color);
+ alpha = 1.0f;
+ }
+
+ return *this;
+ }
+
+ uint8_t RgbaColor::get_alpha(void) const
+ {
+ return alpha;
+ }
+
+ const GLubyte* RgbaColor::get_array(size_t len) const
+ {
+ if (array_length != len && array != NULL) {
+ delete[] array;
+ array = NULL;
+ array_length = 0;
+ }
+
+ if (array == NULL) {
+ int components = get_array_comonent_count();
+ array = new GLubyte[len * components];
+ array_length = len;
+
+ for (size_t i = 0; i < len; i++) {
+ array[i*components] = get_red();
+ array[(i*components) + 1] = get_green();
+ array[(i*components) + 2] = get_blue();
+ array[(i*components) + 3] = alpha;
+ }
+ }
+
+ return array;
+ }
+
+ int RgbaColor::get_array_comonent_count(void) const
+ {
+ return 4;
+ }
+};
diff --git a/src/utils/color/rgba.hpp b/src/utils/color/rgba.hpp
new file mode 100644
index 00000000..5d67a92a
--- /dev/null
+++ b/src/utils/color/rgba.hpp
@@ -0,0 +1,51 @@
+/*
+ * UltraStar Deluxe - Karaoke Game
+ *
+ * UltraStar Deluxe is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef RGBA_COLOR_HPP
+#define RGBA_COLOR_HPP
+
+#include "rgb.hpp"
+
+namespace usdx
+{
+ class RgbaColor : public RgbColor
+ {
+ private:
+ uint8_t alpha;
+
+ public:
+ RgbaColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
+ RgbaColor(const RgbaColor& color);
+ RgbaColor(const RgbColor& color, uint8_t alpha = 255);
+ RgbaColor& operator=(const RgbaColor& color);
+ RgbaColor& operator=(const RgbColor& color);
+
+ uint8_t get_alpha(void) const;
+
+ virtual const GLubyte* get_array(size_t len) const;
+ int get_array_comonent_count(void) const;
+ };
+};
+
+#endif