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