aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/color/hsv.cpp
blob: ecb155e6ec5301b6a7947c8f0f1a6772ba26cdb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
	}

}