diff options
-rw-r--r-- | src/menu/background_color.hpp | 2 | ||||
-rw-r--r-- | src/menu/software_mouse_pointer.hpp | 2 | ||||
-rw-r--r-- | src/menu/static.hpp | 4 | ||||
-rw-r--r-- | src/menu/static_rectangle.hpp | 2 | ||||
-rw-r--r-- | src/menu/texture_colorized.hpp | 2 | ||||
-rw-r--r-- | src/utils/color/hsv.cpp | 105 | ||||
-rw-r--r-- | src/utils/color/hsv.hpp | 66 | ||||
-rw-r--r-- | src/utils/color/rgb.cpp (renamed from src/utils/rgb_color.cpp) | 78 | ||||
-rw-r--r-- | src/utils/color/rgb.hpp (renamed from src/utils/rgb_color.hpp) | 24 | ||||
-rw-r--r-- | src/utils/color/rgba.cpp (renamed from src/utils/rgba_color.cpp) | 26 | ||||
-rw-r--r-- | src/utils/color/rgba.hpp (renamed from src/utils/rgba_color.hpp) | 12 | ||||
-rw-r--r-- | test/Makefile | 4 | ||||
-rw-r--r-- | test/utils/color/hsv.cpp | 87 | ||||
-rw-r--r-- | test/utils/color/rgb.cpp | 88 |
14 files changed, 450 insertions, 52 deletions
diff --git a/src/menu/background_color.hpp b/src/menu/background_color.hpp index 4ba040cb..79059471 100644 --- a/src/menu/background_color.hpp +++ b/src/menu/background_color.hpp @@ -28,7 +28,7 @@ #include <GL/gl.h> #include "background.hpp" -#include "utils/rgb_color.hpp" +#include "utils/color/rgb.hpp" #include "utils/dimension.hpp" namespace usdx diff --git a/src/menu/software_mouse_pointer.hpp b/src/menu/software_mouse_pointer.hpp index 91ba294b..ff9fe4a4 100644 --- a/src/menu/software_mouse_pointer.hpp +++ b/src/menu/software_mouse_pointer.hpp @@ -30,7 +30,7 @@ #include "event_manager.hpp" #include "texture.hpp" #include "timer.hpp" -#include "utils/rgba_color.hpp" +#include "utils/color/rgba.hpp" #include "utils/disposer.hpp" #include <boost/signals2.hpp> diff --git a/src/menu/static.hpp b/src/menu/static.hpp index 6f5ce59a..d8ba2066 100644 --- a/src/menu/static.hpp +++ b/src/menu/static.hpp @@ -26,8 +26,8 @@ #define STATIC_HPP #include "drawable.hpp" -#include "utils/rgb_color.hpp" -#include "utils/rgba_color.hpp" +#include "utils/color/rgb.hpp" +#include "utils/color/rgba.hpp" namespace usdx { diff --git a/src/menu/static_rectangle.hpp b/src/menu/static_rectangle.hpp index fd9b88ca..7cee1a94 100644 --- a/src/menu/static_rectangle.hpp +++ b/src/menu/static_rectangle.hpp @@ -27,7 +27,7 @@ #include "static.hpp" #include "utils/dimension.hpp" -#include "utils/rgb_color.hpp" +#include "utils/color/rgb.hpp" namespace usdx { diff --git a/src/menu/texture_colorized.hpp b/src/menu/texture_colorized.hpp index 10c08ac4..9259bf55 100644 --- a/src/menu/texture_colorized.hpp +++ b/src/menu/texture_colorized.hpp @@ -26,7 +26,7 @@ #define TEXTURE_COLORIZED_HPP #include "texture_transparent.hpp" -#include "utils/rgb_color.hpp" +#include "utils/color/rgb.hpp" namespace usdx { 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/rgb_color.cpp b/src/utils/color/rgb.cpp index 0ebfe67c..c43b85c4 100644 --- a/src/utils/rgb_color.cpp +++ b/src/utils/color/rgb.cpp @@ -24,18 +24,20 @@ #include <cstddef> -#include "rgb_color.hpp" +#include "rgb.hpp" +#include "hsv.hpp" namespace usdx { - RgbColor::RgbColor(int red, int green, int blue) - : red(red / 255.0f), green(green / 255.0f), blue(blue / 255.0f), + RgbColor::RgbColor(uint8_t red, uint8_t green, uint8_t blue) + : red(red), green(green), blue(blue), array(NULL), array_length(0) { } - RgbColor::RgbColor(float red, float green, float 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) { } @@ -45,6 +47,45 @@ namespace usdx { } + 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) { @@ -70,17 +111,17 @@ namespace usdx return *this; } - float RgbColor::get_red(void) const + uint8_t RgbColor::get_red(void) const { return red; } - float RgbColor::get_green(void) const + uint8_t RgbColor::get_green(void) const { return green; } - float RgbColor::get_blue(void) const + uint8_t RgbColor::get_blue(void) const { return blue; } @@ -99,9 +140,9 @@ namespace usdx array_length = len; for (size_t i = 0; i < len; i++) { - array[i*components] = red * 255.0f; - array[(i*components) + 1] = green * 255.0f; - array[(i*components) + 2] = blue * 255.0f; + array[i*components] = red; + array[(i*components) + 1] = green; + array[(i*components) + 2] = blue; } } @@ -112,4 +153,19 @@ namespace usdx { 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/rgb_color.hpp b/src/utils/color/rgb.hpp index 36a5b42c..6871e452 100644 --- a/src/utils/rgb_color.hpp +++ b/src/utils/color/rgb.hpp @@ -26,33 +26,41 @@ #define RGB_COLOR_HPP #include <GL/gl.h> +#include <stdint.h> namespace usdx { + class HsvColor; + class RgbColor { private: - float red; - float green; - float blue; + uint8_t red; + uint8_t green; + uint8_t blue; protected: mutable GLubyte *array; mutable size_t array_length; public: - RgbColor(int red, int green, int blue); - RgbColor(float red, float green, float blue); + 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); - float get_red(void) const; - float get_green(void) const; - float get_blue(void) const; + 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; }; }; diff --git a/src/utils/rgba_color.cpp b/src/utils/color/rgba.cpp index aed484f6..777bb720 100644 --- a/src/utils/rgba_color.cpp +++ b/src/utils/color/rgba.cpp @@ -24,16 +24,11 @@ #include <cstddef> -#include "rgba_color.hpp" +#include "rgba.hpp" namespace usdx { - RgbaColor::RgbaColor(int red, int green, int blue, int alpha) - : RgbColor(red, green, blue), alpha(alpha/255.0f) - { - } - - RgbaColor::RgbaColor(float red, float green, float blue, float alpha) + RgbaColor::RgbaColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) : RgbColor(red, green, blue), alpha(alpha) { } @@ -43,12 +38,7 @@ namespace usdx { } - RgbaColor::RgbaColor(const RgbColor& color, int alpha) - : RgbColor(color), alpha(alpha / 255.0f) - { - } - - RgbaColor::RgbaColor(const RgbColor& color, float alpha) + RgbaColor::RgbaColor(const RgbColor& color, uint8_t alpha) : RgbColor(color), alpha(alpha) { } @@ -73,7 +63,7 @@ namespace usdx return *this; } - float RgbaColor::get_alpha(void) const + uint8_t RgbaColor::get_alpha(void) const { return alpha; } @@ -92,10 +82,10 @@ namespace usdx array_length = len; for (size_t i = 0; i < len; i++) { - array[i*components] = get_red() * 255.0f; - array[(i*components) + 1] = get_green() * 255.0f; - array[(i*components) + 2] = get_blue() * 255.0f; - array[(i*components) + 3] = alpha * 255.0f; + array[i*components] = get_red(); + array[(i*components) + 1] = get_green(); + array[(i*components) + 2] = get_blue(); + array[(i*components) + 3] = alpha; } } diff --git a/src/utils/rgba_color.hpp b/src/utils/color/rgba.hpp index 560727f0..5d67a92a 100644 --- a/src/utils/rgba_color.hpp +++ b/src/utils/color/rgba.hpp @@ -25,25 +25,23 @@ #ifndef RGBA_COLOR_HPP #define RGBA_COLOR_HPP -#include "rgb_color.hpp" +#include "rgb.hpp" namespace usdx { class RgbaColor : public RgbColor { private: - float alpha; + uint8_t alpha; public: - RgbaColor(int red, int green, int blue, int alpha); - RgbaColor(float red, float green, float blue, float alpha); + RgbaColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); RgbaColor(const RgbaColor& color); - RgbaColor(const RgbColor& color, int alpha); - RgbaColor(const RgbColor& color, float alpha); + RgbaColor(const RgbColor& color, uint8_t alpha = 255); RgbaColor& operator=(const RgbaColor& color); RgbaColor& operator=(const RgbColor& color); - float get_alpha(void) const; + uint8_t get_alpha(void) const; virtual const GLubyte* get_array(size_t len) const; int get_array_comonent_count(void) const; diff --git a/test/Makefile b/test/Makefile index cfb63f06..57b9df50 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,8 +1,8 @@ TARGET:=usdx-tests include ../Makefile.inc -headers+=$(wildcard *.hpp) $(wildcard */*.hpp) -sources+=$(wildcard *.cpp) $(wildcard */*.cpp) +headers+=$(wildcard *.hpp) $(wildcard */*.hpp) $(wildcard */*/*.hpp) +sources+=$(wildcard *.cpp) $(wildcard */*.cpp) $(wildcard */*/*.cpp) LDFLAGS+=-lcppunit CXXFLAGS-COVERAGE:=-fprofile-arcs -ftest-coverage diff --git a/test/utils/color/hsv.cpp b/test/utils/color/hsv.cpp new file mode 100644 index 00000000..0dda6719 --- /dev/null +++ b/test/utils/color/hsv.cpp @@ -0,0 +1,87 @@ +/* + * 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" + +#include <exception> +#include <cppunit/extensions/HelperMacros.h> +#include <log4cpp/Category.hh> + +namespace usdx +{ + class HsvColorTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(HsvColorTest); + CPPUNIT_TEST(testConversionBlack); + CPPUNIT_TEST(testConversionGray); + CPPUNIT_TEST(testConversionFromRgb); + CPPUNIT_TEST_SUITE_END(); + private: + static log4cpp::Category& log; + public: + void setUp() + { + } + + void tearDown() + { + } + + void testConversionBlack() + { + RgbColor rgb(0, 0, 0); + HsvColor hsv(rgb); + + CPPUNIT_ASSERT_EQUAL(0, (int)hsv.get_hue()); + CPPUNIT_ASSERT_EQUAL(0, (int)hsv.get_saturation()); + CPPUNIT_ASSERT_EQUAL(0, (int)hsv.get_value()); + } + + void testConversionGray() + { + RgbColor rgb(123, 123, 123); + HsvColor hsv(rgb); + + CPPUNIT_ASSERT_EQUAL(0, (int)hsv.get_hue()); + CPPUNIT_ASSERT_EQUAL(0, (int)hsv.get_saturation()); + CPPUNIT_ASSERT_EQUAL(123, (int)hsv.get_value()); + } + + void testConversionFromRgb() + { + RgbColor rgb(112, 172, 182); + HsvColor hsv(rgb); + + CPPUNIT_ASSERT_EQUAL(135, (int)hsv.get_hue()); + CPPUNIT_ASSERT_EQUAL(98, (int)hsv.get_saturation()); + CPPUNIT_ASSERT_EQUAL(182, (int)hsv.get_value()); + } + }; + + log4cpp::Category& HsvColorTest::log = + log4cpp::Category::getInstance("test.usdx.utils.color.hsv"); + + CPPUNIT_TEST_SUITE_REGISTRATION(HsvColorTest); + +}; diff --git a/test/utils/color/rgb.cpp b/test/utils/color/rgb.cpp new file mode 100644 index 00000000..68037973 --- /dev/null +++ b/test/utils/color/rgb.cpp @@ -0,0 +1,88 @@ +/* + * 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" + +#include <exception> +#include <cppunit/extensions/HelperMacros.h> +#include <log4cpp/Category.hh> + +namespace usdx +{ + class RgbColorTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(RgbColorTest); + CPPUNIT_TEST(testConversionFromHsv); + CPPUNIT_TEST(testConversionFromHsvBlack); + CPPUNIT_TEST(testConversionFromHsvGray); + CPPUNIT_TEST_SUITE_END(); + private: + static log4cpp::Category& log; + public: + void setUp() + { + } + + void tearDown() + { + } + + void testConversionFromHsv() + { + HsvColor hsv(135, 98, 182); + RgbColor rgb(hsv); + + // original values r: 112 g: 172 b: 182 + CPPUNIT_ASSERT_EQUAL(128, (int)rgb.get_red()); + CPPUNIT_ASSERT_EQUAL(111, (int)rgb.get_green()); + CPPUNIT_ASSERT_EQUAL(182, (int)rgb.get_blue()); + } + + void testConversionFromHsvBlack() + { + HsvColor hsv(0, 0, 0); + RgbColor rgb(hsv); + + CPPUNIT_ASSERT_EQUAL(0, (int)rgb.get_red()); + CPPUNIT_ASSERT_EQUAL(0, (int)rgb.get_green()); + CPPUNIT_ASSERT_EQUAL(0, (int)rgb.get_blue()); + } + + void testConversionFromHsvGray() + { + HsvColor hsv(0, 0, 123); + RgbColor rgb(hsv); + + CPPUNIT_ASSERT_EQUAL(123, (int)rgb.get_red()); + CPPUNIT_ASSERT_EQUAL(123, (int)rgb.get_green()); + CPPUNIT_ASSERT_EQUAL(123, (int)rgb.get_blue()); + } + }; + + log4cpp::Category& RgbColorTest::log = + log4cpp::Category::getInstance("test.usdx.utils.color.rgb"); + + CPPUNIT_TEST_SUITE_REGISTRATION(RgbColorTest); + +}; |