aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/color/rgb.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/utils/color/rgb.cpp (renamed from src/utils/rgb_color.cpp)78
1 files changed, 67 insertions, 11 deletions
diff --git a/src/utils/rgb_color.cpp b/src/utils/color/rgb.cpp
index 0ebfe67c..c43b85c4 100644
--- a/src/utils/rgb_color.cpp
+++ b/src/utils/color/rgb.cpp
@@ -24,18 +24,20 @@
#include <cstddef>
-#include "rgb_color.hpp"
+#include "rgb.hpp"
+#include "hsv.hpp"
namespace usdx
{
- RgbColor::RgbColor(int red, int green, int blue)
- : red(red / 255.0f), green(green / 255.0f), blue(blue / 255.0f),
+ RgbColor::RgbColor(uint8_t red, uint8_t green, uint8_t blue)
+ : red(red), green(green), blue(blue),
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 uint32_t color)
+ : red((color & 0xff0000) >> 16), green((color & 0xff00) >> 8),
+ blue(color & 0xff), array(NULL), array_length(0)
{
}
@@ -45,6 +47,45 @@ namespace usdx
{
}
+ 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) {
@@ -70,17 +111,17 @@ namespace usdx
return *this;
}
- float RgbColor::get_red(void) const
+ uint8_t RgbColor::get_red(void) const
{
return red;
}
- float RgbColor::get_green(void) const
+ uint8_t RgbColor::get_green(void) const
{
return green;
}
- float RgbColor::get_blue(void) const
+ uint8_t RgbColor::get_blue(void) const
{
return blue;
}
@@ -99,9 +140,9 @@ namespace usdx
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;
+ array[i*components] = red;
+ array[(i*components) + 1] = green;
+ array[(i*components) + 2] = blue;
}
}
@@ -112,4 +153,19 @@ namespace usdx
{
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);
+ }
};