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/utils/color/hsv.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/utils/color/hsv.cpp (limited to 'src/utils/color/hsv.cpp') 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; + } + +}; -- cgit v1.2.3