aboutsummaryrefslogblamecommitdiffstats
path: root/src/utils/color/hsv.cpp
blob: 11df9ec2cc2c820ee6dcd0e2f083f4d3521aab9c (plain) (tree)








































































































                                                                                                       
/*
 * 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;
	}

};