From ac164afa0954e28b521dc6b19d2ccf0320cc8ed1 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 8 Apr 2014 05:17:27 +0200 Subject: 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). --- src/menu/background_color.hpp | 2 +- src/menu/software_mouse_pointer.hpp | 2 +- src/menu/static.hpp | 4 +- src/menu/static_rectangle.hpp | 2 +- src/menu/texture_colorized.hpp | 2 +- src/utils/color/hsv.cpp | 105 ++++++++++++++++++++++ src/utils/color/hsv.hpp | 66 ++++++++++++++ src/utils/color/rgb.cpp | 171 ++++++++++++++++++++++++++++++++++++ src/utils/color/rgb.hpp | 67 ++++++++++++++ src/utils/color/rgba.cpp | 99 +++++++++++++++++++++ src/utils/color/rgba.hpp | 51 +++++++++++ src/utils/rgb_color.cpp | 115 ------------------------ src/utils/rgb_color.hpp | 59 ------------- src/utils/rgba_color.cpp | 109 ----------------------- src/utils/rgba_color.hpp | 53 ----------- test/Makefile | 4 +- test/utils/color/hsv.cpp | 87 ++++++++++++++++++ test/utils/color/rgb.cpp | 88 +++++++++++++++++++ 18 files changed, 742 insertions(+), 344 deletions(-) create mode 100644 src/utils/color/hsv.cpp create mode 100644 src/utils/color/hsv.hpp create mode 100644 src/utils/color/rgb.cpp create mode 100644 src/utils/color/rgb.hpp create mode 100644 src/utils/color/rgba.cpp create mode 100644 src/utils/color/rgba.hpp delete mode 100644 src/utils/rgb_color.cpp delete mode 100644 src/utils/rgb_color.hpp delete mode 100644 src/utils/rgba_color.cpp delete mode 100644 src/utils/rgba_color.hpp create mode 100644 test/utils/color/hsv.cpp create mode 100644 test/utils/color/rgb.cpp 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 #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 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 + +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 + +#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 +#include + +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 + +#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 diff --git a/src/utils/rgb_color.cpp b/src/utils/rgb_color.cpp deleted file mode 100644 index 0ebfe67c..00000000 --- a/src/utils/rgb_color.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 - -#include "rgb_color.hpp" - -namespace usdx -{ - RgbColor::RgbColor(int red, int green, int blue) - : red(red / 255.0f), green(green / 255.0f), blue(blue / 255.0f), - 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 RgbColor& color) - : red(color.red), green(color.green), blue(color.blue), array(NULL), - array_length(0) - { - } - - 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; - } - - float RgbColor::get_red(void) const - { - return red; - } - - float RgbColor::get_green(void) const - { - return green; - } - - float 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 * 255.0f; - array[(i*components) + 1] = green * 255.0f; - array[(i*components) + 2] = blue * 255.0f; - } - } - - return array; - } - - int RgbColor::get_array_comonent_count(void) const - { - return 3; - } -}; diff --git a/src/utils/rgb_color.hpp b/src/utils/rgb_color.hpp deleted file mode 100644 index 36a5b42c..00000000 --- a/src/utils/rgb_color.hpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 - -namespace usdx -{ - class RgbColor - { - private: - float red; - float green; - float 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(const RgbColor& color); - virtual ~RgbColor(); - RgbColor& operator=(const RgbColor& color); - - float get_red(void) const; - float get_green(void) const; - float get_blue(void) const; - - virtual const GLubyte* get_array(size_t len) const; - int get_array_comonent_count(void) const; - }; -}; - -#endif diff --git a/src/utils/rgba_color.cpp b/src/utils/rgba_color.cpp deleted file mode 100644 index aed484f6..00000000 --- a/src/utils/rgba_color.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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 - -#include "rgba_color.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) - : RgbColor(red, green, blue), alpha(alpha) - { - } - - RgbaColor::RgbaColor(const RgbaColor& color) - : RgbColor(color), alpha(color.alpha) - { - } - - RgbaColor::RgbaColor(const RgbColor& color, int alpha) - : RgbColor(color), alpha(alpha / 255.0f) - { - } - - RgbaColor::RgbaColor(const RgbColor& color, float 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; - } - - float 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() * 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; - } - } - - return array; - } - - int RgbaColor::get_array_comonent_count(void) const - { - return 4; - } -}; diff --git a/src/utils/rgba_color.hpp b/src/utils/rgba_color.hpp deleted file mode 100644 index 560727f0..00000000 --- a/src/utils/rgba_color.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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_color.hpp" - -namespace usdx -{ - class RgbaColor : public RgbColor - { - private: - float alpha; - - public: - RgbaColor(int red, int green, int blue, int alpha); - RgbaColor(float red, float green, float blue, float alpha); - RgbaColor(const RgbaColor& color); - RgbaColor(const RgbColor& color, int alpha); - RgbaColor(const RgbColor& color, float alpha); - RgbaColor& operator=(const RgbaColor& color); - RgbaColor& operator=(const RgbColor& color); - - float get_alpha(void) const; - - virtual const GLubyte* get_array(size_t len) const; - int get_array_comonent_count(void) const; - }; -}; - -#endif 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 +#include +#include + +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 +#include +#include + +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); + +}; -- cgit v1.2.3