Refactor color utils (#955)

Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
This commit is contained in:
Murat Seker
2020-08-08 13:22:37 +02:00
committed by GitHub
parent 63d95a5a2a
commit a18ccb8b48
20 changed files with 181 additions and 120 deletions

View File

@@ -1,10 +1,9 @@
// Utils includes
#include <utils/ColorArgb.h>
ColorArgb ColorArgb::BLACK = { 255, 0, 0, 0 };
ColorArgb ColorArgb::RED = { 255, 255, 0, 0 };
ColorArgb ColorArgb::GREEN = { 255, 0, 255, 0 };
ColorArgb ColorArgb::BLUE = { 255, 0, 0, 255 };
ColorArgb ColorArgb::YELLOW= { 255, 255, 255, 0 };
ColorArgb ColorArgb::WHITE = { 255, 255, 255, 255 };
const ColorArgb ColorArgb::BLACK = { 255, 0, 0, 0 };
const ColorArgb ColorArgb::RED = { 255, 255, 0, 0 };
const ColorArgb ColorArgb::GREEN = { 255, 0, 255, 0 };
const ColorArgb ColorArgb::BLUE = { 255, 0, 0, 255 };
const ColorArgb ColorArgb::YELLOW = { 255, 255, 255, 0 };
const ColorArgb ColorArgb::WHITE = { 255, 255, 255, 255 };

View File

@@ -1,11 +1,10 @@
// Local includes
#include <utils/ColorBgr.h>
ColorBgr ColorBgr::BLACK = { 0, 0, 0 };
ColorBgr ColorBgr::RED = { 0, 0, 255 };
ColorBgr ColorBgr::GREEN = { 0, 255, 0 };
ColorBgr ColorBgr::BLUE = { 255, 0, 0 };
ColorBgr ColorBgr::YELLOW= { 0, 255, 255 };
ColorBgr ColorBgr::WHITE = { 255, 255, 255 };
const ColorBgr ColorBgr::BLACK = { 0, 0, 0 };
const ColorBgr ColorBgr::RED = { 0, 0, 255 };
const ColorBgr ColorBgr::GREEN = { 0, 255, 0 };
const ColorBgr ColorBgr::BLUE = { 255, 0, 0 };
const ColorBgr ColorBgr::YELLOW = { 0, 255, 255 };
const ColorBgr ColorBgr::WHITE = { 255, 255, 255 };

View File

@@ -1,10 +1,9 @@
// Local includes
#include <utils/ColorRgb.h>
ColorRgb ColorRgb::BLACK = { 0, 0, 0 };
ColorRgb ColorRgb::RED = { 255, 0, 0 };
ColorRgb ColorRgb::GREEN = { 0, 255, 0 };
ColorRgb ColorRgb::BLUE = { 0, 0, 255 };
ColorRgb ColorRgb::YELLOW= { 255, 255, 0 };
ColorRgb ColorRgb::WHITE = { 255, 255, 255 };
const ColorRgb ColorRgb::BLACK = { 0, 0, 0 };
const ColorRgb ColorRgb::RED = { 255, 0, 0 };
const ColorRgb ColorRgb::GREEN = { 0, 255, 0 };
const ColorRgb ColorRgb::BLUE = { 0, 0, 255 };
const ColorRgb ColorRgb::YELLOW = { 255, 255, 0 };
const ColorRgb ColorRgb::WHITE = { 255, 255, 255 };

View File

@@ -1,10 +1,9 @@
// Utils includes
#include <utils/ColorRgba.h>
ColorRgba ColorRgba::BLACK = { 0, 0, 0, 255 };
ColorRgba ColorRgba::RED = { 255, 0, 0, 255 };
ColorRgba ColorRgba::GREEN = { 0, 255, 0, 255 };
ColorRgba ColorRgba::BLUE = { 0, 0, 255, 255 };
ColorRgba ColorRgba::YELLOW= { 255, 255, 0, 255 };
ColorRgba ColorRgba::WHITE = { 255, 255, 255, 255 };
const ColorRgba ColorRgba::BLACK = { 0, 0, 0, 255 };
const ColorRgba ColorRgba::RED = { 255, 0, 0, 255 };
const ColorRgba ColorRgba::GREEN = { 0, 255, 0, 255 };
const ColorRgba ColorRgba::BLUE = { 0, 0, 255, 255 };
const ColorRgba ColorRgba::YELLOW = { 255, 255, 0, 255 };
const ColorRgba ColorRgba::WHITE = { 255, 255, 255, 255 };

View File

@@ -1,10 +1,9 @@
// Local includes
#include <utils/ColorRgbw.h>
ColorRgbw ColorRgbw::BLACK = { 0, 0, 0, 0 };
ColorRgbw ColorRgbw::RED = { 255, 0, 0, 0 };
ColorRgbw ColorRgbw::GREEN = { 0, 255, 0, 0 };
ColorRgbw ColorRgbw::BLUE = { 0, 0, 255, 0 };
ColorRgbw ColorRgbw::YELLOW= { 255, 255, 0, 0 };
ColorRgbw ColorRgbw::WHITE = { 0, 0, 0, 255 };
const ColorRgbw ColorRgbw::BLACK = { 0, 0, 0, 0 };
const ColorRgbw ColorRgbw::RED = { 255, 0, 0, 0 };
const ColorRgbw ColorRgbw::GREEN = { 0, 255, 0, 0 };
const ColorRgbw ColorRgbw::BLUE = { 0, 0, 255, 0 };
const ColorRgbw ColorRgbw::YELLOW = { 255, 255, 0, 0 };
const ColorRgbw ColorRgbw::WHITE = { 0, 0, 0, 255 };

View File

@@ -2,6 +2,11 @@
#include <QColor>
inline uint8_t clamp(int x)
{
return (x<0) ? 0 : ((x>255) ? 255 : uint8_t(x));
}
void ColorSys::rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance)
{
QColor color(red,green,blue);
@@ -35,3 +40,15 @@ void ColorSys::hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value, uint8_t
green = (uint8_t)color.green();
blue = (uint8_t)color.blue();
}
void ColorSys::yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t &r, uint8_t &g, uint8_t &b)
{
// see: http://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
int c = y - 16;
int d = u - 128;
int e = v - 128;
r = clamp((298 * c + 409 * e + 128) >> 8);
g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
b = clamp((298 * c + 516 * d + 128) >> 8);
}

View File

@@ -1,5 +1,5 @@
#include "utils/ImageResampler.h"
#include <utils/ColorSys.h>
#include <utils/Logger.h>
ImageResampler::ImageResampler()
@@ -80,7 +80,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
uint8_t y = data[index+1];
uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2];
uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ];
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
ColorSys::yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
}
break;
case PixelFormat::YUYV:
@@ -89,7 +89,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
uint8_t y = data[index];
uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1];
uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1];
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
ColorSys::yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
}
break;
case PixelFormat::BGR16:
@@ -135,20 +135,3 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
}
}
}
uint8_t ImageResampler::clamp(int x)
{
return (x<0) ? 0 : ((x>255) ? 255 : uint8_t(x));
}
void ImageResampler::yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t &r, uint8_t &g, uint8_t &b)
{
// see: http://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
int c = y - 16;
int d = u - 128;
int e = v - 128;
r = clamp((298 * c + 409 * e + 128) >> 8);
g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
b = clamp((298 * c + 516 * d + 128) >> 8);
}

View File

@@ -14,7 +14,7 @@ WhiteAlgorithm stringToWhiteAlgorithm(QString str)
return WhiteAlgorithm::INVALID;
}
void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, const WhiteAlgorithm algorithm)
void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, WhiteAlgorithm algorithm)
{
switch (algorithm)
{