mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
- HSV transform added
- Moved transform of led values to before the device write so transform changes are taken into account
This commit is contained in:
@@ -19,6 +19,7 @@ SET(Hyperion_HEADERS
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
|
||||
${CURRENT_SOURCE_DIR}/ImageToLedsMap.h
|
||||
${CURRENT_SOURCE_DIR}/ColorTransform.h
|
||||
${CURRENT_SOURCE_DIR}/HsvTransform.h
|
||||
)
|
||||
|
||||
SET(Hyperion_SOURCES
|
||||
@@ -32,6 +33,7 @@ SET(Hyperion_SOURCES
|
||||
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
|
||||
${CURRENT_SOURCE_DIR}/ImageToLedsMap.cpp
|
||||
${CURRENT_SOURCE_DIR}/ColorTransform.cpp
|
||||
${CURRENT_SOURCE_DIR}/HsvTransform.cpp
|
||||
)
|
||||
|
||||
QT4_WRAP_CPP(Hyperion_HEADERS_MOC ${Hyperion_QT_HEADERS})
|
||||
|
134
libsrc/hyperion/HsvTransform.cpp
Normal file
134
libsrc/hyperion/HsvTransform.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "HsvTransform.h"
|
||||
|
||||
using namespace hyperion;
|
||||
|
||||
HsvTransform::HsvTransform() :
|
||||
_saturationGain(1.0),
|
||||
_valueGain(1.0)
|
||||
{
|
||||
}
|
||||
|
||||
HsvTransform::HsvTransform(double saturationGain, double valueGain) :
|
||||
_saturationGain(saturationGain),
|
||||
_valueGain(valueGain)
|
||||
{
|
||||
}
|
||||
|
||||
HsvTransform::~HsvTransform()
|
||||
{
|
||||
}
|
||||
|
||||
void HsvTransform::setSaturationGain(double saturationGain)
|
||||
{
|
||||
_saturationGain = saturationGain;
|
||||
}
|
||||
|
||||
double HsvTransform::getSaturationGain() const
|
||||
{
|
||||
return _saturationGain;
|
||||
}
|
||||
|
||||
void HsvTransform::setValueGain(double valueGain)
|
||||
{
|
||||
_valueGain = valueGain;
|
||||
}
|
||||
|
||||
double HsvTransform::getValueGain() const
|
||||
{
|
||||
return _valueGain;
|
||||
}
|
||||
|
||||
void HsvTransform::transform(uint8_t & red, uint8_t & green, uint8_t & blue) const
|
||||
{
|
||||
if (_saturationGain != 1.0 || _valueGain != 1.0)
|
||||
{
|
||||
uint8_t hue, saturation, value;
|
||||
rgb2hsv(red, green, blue, hue, saturation, value);
|
||||
|
||||
int s = saturation * _saturationGain;
|
||||
if (s > 255)
|
||||
saturation = 255;
|
||||
else
|
||||
saturation = s;
|
||||
|
||||
int v = value * _valueGain;
|
||||
if (v > 255)
|
||||
value = 255;
|
||||
else
|
||||
value = v;
|
||||
|
||||
hsv2rgb(hue, saturation, value, red, green, blue);
|
||||
}
|
||||
}
|
||||
|
||||
void HsvTransform::rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint8_t & hue, uint8_t & saturation, uint8_t & value)
|
||||
{
|
||||
uint8_t rgbMin, rgbMax;
|
||||
|
||||
rgbMin = red < green ? (red < blue ? red : blue) : (green < blue ? green : blue);
|
||||
rgbMax = red > green ? (red > blue ? red : blue) : (green > blue ? green : blue);
|
||||
|
||||
value = rgbMax;
|
||||
if (value == 0)
|
||||
{
|
||||
hue = 0;
|
||||
saturation = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
saturation = 255 * long(rgbMax - rgbMin) / value;
|
||||
if (saturation == 0)
|
||||
{
|
||||
hue = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (rgbMax == red)
|
||||
hue = 0 + 43 * (green - blue) / (rgbMax - rgbMin);
|
||||
else if (rgbMax == green)
|
||||
hue = 85 + 43 * (blue - red) / (rgbMax - rgbMin);
|
||||
else
|
||||
hue = 171 + 43 * (red - green) / (rgbMax - rgbMin);
|
||||
}
|
||||
|
||||
void HsvTransform::hsv2rgb(uint8_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue)
|
||||
{
|
||||
uint8_t region, remainder, p, q, t;
|
||||
|
||||
if (saturation == 0)
|
||||
{
|
||||
red = value;
|
||||
green = value;
|
||||
blue = value;
|
||||
return;
|
||||
}
|
||||
|
||||
region = hue / 43;
|
||||
remainder = (hue - (region * 43)) * 6;
|
||||
|
||||
p = (value * (255 - saturation)) >> 8;
|
||||
q = (value * (255 - ((saturation * remainder) >> 8))) >> 8;
|
||||
t = (value * (255 - ((saturation * (255 - remainder)) >> 8))) >> 8;
|
||||
|
||||
switch (region)
|
||||
{
|
||||
case 0:
|
||||
red = value; green = t; blue = p;
|
||||
break;
|
||||
case 1:
|
||||
red = q; green = value; blue = p;
|
||||
break;
|
||||
case 2:
|
||||
red = p; green = value; blue = t;
|
||||
break;
|
||||
case 3:
|
||||
red = p; green = q; blue = value;
|
||||
break;
|
||||
case 4:
|
||||
red = t; green = p; blue = value;
|
||||
break;
|
||||
default:
|
||||
red = value; green = p; blue = q;
|
||||
break;
|
||||
}
|
||||
}
|
33
libsrc/hyperion/HsvTransform.h
Normal file
33
libsrc/hyperion/HsvTransform.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
|
||||
class HsvTransform
|
||||
{
|
||||
public:
|
||||
HsvTransform();
|
||||
HsvTransform(double saturationGain, double valueGain);
|
||||
~HsvTransform();
|
||||
|
||||
void setSaturationGain(double saturationGain);
|
||||
double getSaturationGain() const;
|
||||
|
||||
void setValueGain(double valueGain);
|
||||
double getValueGain() const;
|
||||
|
||||
void transform(uint8_t & red, uint8_t & green, uint8_t & blue) const;
|
||||
|
||||
private:
|
||||
// integer version of the conversion are faster, but a little less accurate
|
||||
static void rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint8_t & hue, uint8_t & saturation, uint8_t & value);
|
||||
static void hsv2rgb(uint8_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue);
|
||||
|
||||
private:
|
||||
double _saturationGain;
|
||||
double _valueGain;
|
||||
};
|
||||
|
||||
} // namespace hyperion
|
@@ -15,6 +15,7 @@
|
||||
#include "LedDeviceWs2801.h"
|
||||
#include "LedDeviceTest.h"
|
||||
#include "ColorTransform.h"
|
||||
#include "HsvTransform.h"
|
||||
|
||||
using namespace hyperion;
|
||||
|
||||
@@ -45,6 +46,11 @@ LedDevice* constructDevice(const Json::Value& deviceConfig)
|
||||
return device;
|
||||
}
|
||||
|
||||
HsvTransform * createHsvTransform(const Json::Value & hsvConfig)
|
||||
{
|
||||
return new HsvTransform(hsvConfig["saturationGain"].asDouble(), hsvConfig["valueGain"].asDouble());
|
||||
}
|
||||
|
||||
ColorTransform* createColorTransform(const Json::Value& colorConfig)
|
||||
{
|
||||
const double threshold = colorConfig["threshold"].asDouble();
|
||||
@@ -78,6 +84,7 @@ LedString Hyperion::createLedString(const Json::Value& ledsConfig)
|
||||
Hyperion::Hyperion(const Json::Value &jsonConfig) :
|
||||
_ledString(createLedString(jsonConfig["leds"])),
|
||||
_muxer(_ledString.leds().size()),
|
||||
_hsvTransform(createHsvTransform(jsonConfig["color"]["hsv"])),
|
||||
_redTransform(createColorTransform(jsonConfig["color"]["red"])),
|
||||
_greenTransform(createColorTransform(jsonConfig["color"]["green"])),
|
||||
_blueTransform(createColorTransform(jsonConfig["color"]["blue"])),
|
||||
@@ -99,6 +106,9 @@ Hyperion::~Hyperion()
|
||||
// Delete the Led-String
|
||||
delete _device;
|
||||
|
||||
// delete he hsv transform
|
||||
delete _hsvTransform;
|
||||
|
||||
// Delete the color-transform
|
||||
delete _blueTransform;
|
||||
delete _greenTransform;
|
||||
@@ -121,14 +131,6 @@ void Hyperion::setColor(int priority, RgbColor & color, const int timeout_ms)
|
||||
|
||||
void Hyperion::setColors(int priority, std::vector<RgbColor>& ledColors, const int timeout_ms)
|
||||
{
|
||||
// Apply the transform to each led and color-channel
|
||||
for (RgbColor& color : ledColors)
|
||||
{
|
||||
color.red = _redTransform->transform(color.red);
|
||||
color.green = _greenTransform->transform(color.green);
|
||||
color.blue = _blueTransform->transform(color.blue);
|
||||
}
|
||||
|
||||
if (timeout_ms > 0)
|
||||
{
|
||||
const uint64_t timeoutTime = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
|
||||
@@ -161,27 +163,40 @@ void Hyperion::setTransform(Hyperion::Transform transform, Hyperion::Color color
|
||||
t = _blueTransform;
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
// set transform value
|
||||
switch (transform)
|
||||
{
|
||||
case SATURATION_GAIN:
|
||||
_hsvTransform->setSaturationGain(value);
|
||||
break;
|
||||
case VALUE_GAIN:
|
||||
_hsvTransform->setValueGain(value);
|
||||
break;
|
||||
case THRESHOLD:
|
||||
assert (t != nullptr);
|
||||
t->setThreshold(value);
|
||||
break;
|
||||
case GAMMA:
|
||||
assert (t != nullptr);
|
||||
t->setGamma(value);
|
||||
break;
|
||||
case BLACKLEVEL:
|
||||
assert (t != nullptr);
|
||||
t->setBlacklevel(value);
|
||||
break;
|
||||
case WHITELEVEL:
|
||||
assert (t != nullptr);
|
||||
t->setWhitelevel(value);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// update the led output
|
||||
update();
|
||||
}
|
||||
|
||||
void Hyperion::clear(int priority)
|
||||
@@ -222,19 +237,27 @@ double Hyperion::getTransform(Hyperion::Transform transform, Hyperion::Color col
|
||||
t = _blueTransform;
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
// set transform value
|
||||
switch (transform)
|
||||
{
|
||||
case SATURATION_GAIN:
|
||||
return _hsvTransform->getSaturationGain();
|
||||
case VALUE_GAIN:
|
||||
return _hsvTransform->getValueGain();
|
||||
case THRESHOLD:
|
||||
assert (t != nullptr);
|
||||
return t->getThreshold();
|
||||
case GAMMA:
|
||||
assert (t != nullptr);
|
||||
return t->getGamma();
|
||||
case BLACKLEVEL:
|
||||
assert (t != nullptr);
|
||||
return t->getBlacklevel();
|
||||
case WHITELEVEL:
|
||||
assert (t != nullptr);
|
||||
return t->getWhitelevel();
|
||||
default:
|
||||
assert(false);
|
||||
@@ -262,8 +285,18 @@ void Hyperion::update()
|
||||
int priority = _muxer.getCurrentPriority();
|
||||
const PriorityMuxer::InputInfo & priorityInfo = _muxer.getInputInfo(priority);
|
||||
|
||||
// Apply the transform to each led and color-channel
|
||||
std::vector<RgbColor> ledColors(priorityInfo.ledColors);
|
||||
for (RgbColor& color : ledColors)
|
||||
{
|
||||
_hsvTransform->transform(color.red, color.green, color.blue);
|
||||
color.red = _redTransform->transform(color.red);
|
||||
color.green = _greenTransform->transform(color.green);
|
||||
color.blue = _blueTransform->transform(color.blue);
|
||||
}
|
||||
|
||||
// Write the data to the device
|
||||
_device->write(priorityInfo.ledColors);
|
||||
_device->write(ledColors);
|
||||
|
||||
// Start the timeout-timer
|
||||
if (priorityInfo.timeoutTime_ms == -1)
|
||||
|
Reference in New Issue
Block a user