Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed May 20, 2021
1 parent 3bd752e commit 53afb65
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 125 deletions.
11 changes: 5 additions & 6 deletions src/modm/ui/color/brightness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ class Rgb565;
*
* @author Thomas Sommer
*
* \tparam T Unsigned integral for the brightness-value
* @tparam T Unsigned integral for the brightness-value
* @ingroup modm_ui_color
*/

template<std::unsigned_integral T>
class BrightnessT
{
Expand All @@ -55,17 +54,17 @@ class BrightnessT
* Copy Constructor 8bit->16bit
*/
template<std::unsigned_integral U>
requires std::is_same_v<T, uint16_t> && std::is_same_v<U, uint8_t>
constexpr BrightnessT(const BrightnessT<U> &brightness_other)
requires std::is_same_v<T, uint16_t> && std::is_same_v<U, uint8_t>
constexpr BrightnessT(const BrightnessT<U> &brightness_other)
: value(brightness_other.value << 8)
{}

/**
* Copy Constructor 16bit->8bit
*/
template<std::unsigned_integral U>
requires std::is_same_v<T, uint8_t> && std::is_same_v<U, uint16_t>
constexpr BrightnessT(const BrightnessT<U> &brightness_other)
requires std::is_same_v<T, uint8_t> && std::is_same_v<U, uint16_t>
constexpr BrightnessT(const BrightnessT<U> &brightness_other)
: value(brightness_other.value >> 8)
{}

Expand Down
10 changes: 4 additions & 6 deletions src/modm/ui/color/hsv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class Rgb565;
* @author Martin Rosekeit, Fabian Greif, Niklas Hauser, David Hebbeker, Thomas Sommer
* @ingroup modm_ui_color
*/

// TODO conecpt to only allow unsigned int or float
template<std::unsigned_integral T>
class HsvT
{
Expand All @@ -59,17 +57,17 @@ class HsvT
* Copy Constructor 8bit->16bit
*/
template<std::unsigned_integral U>
requires std::is_same_v<T, uint16_t> && std::is_same_v<U, uint8_t>
constexpr HsvT(const HsvT<U> &hsv_other)
requires std::is_same_v<T, uint16_t> && std::is_same_v<U, uint8_t>
constexpr HsvT(const HsvT<U> &hsv_other)
: hue(hsv_other.hue << 8), saturation(hsv_other.saturation << 8), value(hsv_other.value << 8)
{}

/**
* Copy Constructor 16bit->8bit
*/
template<std::unsigned_integral U>
requires std::is_same_v<T, uint8_t> && std::is_same_v<U, uint16_t>
constexpr HsvT(const HsvT<U> &hsv_other)
requires std::is_same_v<T, uint8_t> && std::is_same_v<U, uint16_t>
constexpr HsvT(const HsvT<U> &hsv_other)
: hue(hsv_other.hue >> 8), saturation(hsv_other.saturation >> 8), value(hsv_other.value >> 8)
{}

Expand Down
102 changes: 28 additions & 74 deletions src/modm/ui/color/rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ class RgbT
* Copy Constructor 8bit->16bit
*/
template<std::unsigned_integral U>
requires std::is_same_v<T, uint16_t> && std::is_same_v<U, uint8_t>
constexpr RgbT(const RgbT<U> &rgb_other)
requires std::is_same_v<T, uint16_t> && std::is_same_v<U, uint8_t>
constexpr RgbT(const RgbT<U> &rgb_other)
: red(rgb_other.red << 8), green(rgb_other.green << 8), blue(rgb_other.blue << 8)
{}

/**
* Copy Constructor 16bit->8bit
*/
template<std::unsigned_integral U>
requires std::is_same_v<T, uint8_t> && std::is_same_v<U, uint16_t>
constexpr RgbT(const RgbT<U> &rgb_other)
requires std::is_same_v<T, uint8_t> && std::is_same_v<U, uint16_t>
constexpr RgbT(const RgbT<U> &rgb_other)
: red(rgb_other.red >> 8), green(rgb_other.green >> 8), blue(rgb_other.blue >> 8)
{}

Expand Down Expand Up @@ -108,76 +108,6 @@ class RgbT
blue(rgb565.color << 3)
{}

TSum
getColorSum() const
{
return red + green + blue;
}

// TODO move getRelative* out of RgbT, it's way to special
template<typename IntermediateType = float, std::unsigned_integral ReturnType = T>
requires std::is_fundamental_v<IntermediateType>
ReturnType
getRelative(const T color, IntermediateType multiplier = 100) const
{
return IntermediateType(color) * multiplier / IntermediateType(getColorSum());
}

template<typename IntermediateType = float, std::unsigned_integral ReturnType = T>
requires std::is_fundamental_v<IntermediateType>
ReturnType
getRelativeRed(IntermediateType multiplier = 100) const
{
return getRelative<IntermediateType, ReturnType>(red, multiplier);
}

template<typename IntermediateType = float, std::unsigned_integral ReturnType = T>
requires std::is_fundamental_v<IntermediateType>
ReturnType
getRelativeGreen(IntermediateType multiplier = 100) const
{
return getRelative<IntermediateType, ReturnType>(green, multiplier);
}

template<typename IntermediateType = float, std::unsigned_integral ReturnType = T>
requires std::is_fundamental_v<IntermediateType>
ReturnType
getRelativeBlue(IntermediateType multiplier = 100) const
{
return getRelative<IntermediateType, ReturnType>(blue, multiplier);
}

template<typename IntermediateType = float, std::unsigned_integral ReturnType = T>
requires std::is_fundamental_v<IntermediateType>
RgbT<ReturnType>
getRelativeColors(IntermediateType multiplier = 100) const
{
return RgbT<ReturnType>(getRelativeRed<IntermediateType, ReturnType>(multiplier),
getRelativeGreen<IntermediateType, ReturnType>(multiplier),
getRelativeBlue<IntermediateType, ReturnType>(multiplier));
}

/**
* Normalize color values based on a clear value
*
* Imagine a low band light, for example a green laser. In case the filters
* of a sensor do not transfer this wavelength well, it might result in all
* colors being very low. An aditional clear value (provided f.e. by TCS3472)
* will not filter colors and thus it will see a bright light (intensity).
* In order to still have some signal the very low green value can be
* amplified with the clear value.
*/
template<typename IntermediateType = float>
requires std::is_fundamental_v<IntermediateType>
void
normalizeByValue(T value)
{
const IntermediateType scalar = value / IntermediateType(getColorSum());
red *= scalar;
green *= scalar;
blue *= scalar;
}

constexpr bool
operator==(const RgbT<T>& other) const = default;

Expand All @@ -191,6 +121,30 @@ class RgbT
using Rgb = RgbT<uint8_t>;


/**
* Normalize color values based on a clear value
*
* Imagine a low band light, for example a green laser. In case the filters
* of a sensor do not transfer this wavelength well, it might result in all
* colors being very low. An aditional clear value (provided f.e. by TCS3472)
* will not filter colors and thus it will see a bright light (intensity).
* In order to still have some signal the very low green value can be
* amplified with the clear value.
*
* @ingroup modm_ui_color
*/
template<std::unsigned_integral T, typename IntermediateType = float, std::unsigned_integral ReturnType = T>
requires std::is_fundamental_v<IntermediateType>
constexpr RgbT<ReturnType>
normalizeColor(RgbT<T> rgb, IntermediateType multiplier = 1) const
{
const IntermediateType sum = IntermediateType(rgb.red) + rgb.green + rgb.blue;
return RgbT<ReturnType>(IntermediateType(rgb.red) * multiplier / sum,
IntermediateType(rgb.green) * multiplier / sum,
IntermediateType(rgb.blue) * multiplier / sum);
}


#if __has_include(<modm/io/iostream.hpp>)
#include <modm/io/iostream.hpp>

Expand Down
16 changes: 0 additions & 16 deletions src/modm/ui/color/rgb565.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,6 @@ class Rgb565

return Rgb565(red | green | blue);
}

/// Saturated multiplication ⊗
Rgb565
operator*(const Rgb565 other) const
{
(void)other;
return Rgb565(0);
}

/// Saturated division ⊘
Rgb565
operator/(const Rgb565 other) const
{
(void)other;
return Rgb565(0);
}
};

} // namespace modm::color
49 changes: 26 additions & 23 deletions src/modm/ui/color/rgb_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,32 @@

#include <algorithm>

namespace modm::color {
// TODO Finish generalisation for uint16_t
template<std::unsigned_integral T>
template<std::unsigned_integral U>
constexpr RgbT<T>::RgbT(const HsvT<U> &hsv)
{
uint16_t vs = hsv.value * hsv.saturation;
uint16_t h6 = 6 * hsv.hue;
namespace modm::color
{

// TODO Finish generalisation for uint16_t
template<std::unsigned_integral T>
template<std::unsigned_integral U>
constexpr RgbT<T>::RgbT(const HsvT<U> &hsv)
{
uint16_t vs = hsv.value * hsv.saturation;
uint16_t h6 = 6 * hsv.hue;

T p = ((hsv.value << 8) - vs) >> 8;
T i = h6 >> 8;
uint16_t f = ((i | 1) << 8) - h6;
if (i & 1) { f = -f; }
T u = (((uint32_t)hsv.value << 16) - (uint32_t)vs * f) >> 16;
T p = ((hsv.value << 8) - vs) >> 8;
T i = h6 >> 8;
uint16_t f = ((i | 1) << 8) - h6;
if (i & 1) { f = -f; }
T u = (((uint32_t)hsv.value << 16) - (uint32_t)vs * f) >> 16;

switch (i)
{
case 0: red = hsv.value; green = u; blue = p; break;
case 1: red = u; green = hsv.value; blue = p; break;
case 2: red = p; green = hsv.value; blue = u; break;
case 3: red = p; green = u; blue = hsv.value; break;
case 4: red = u; green = p; blue = hsv.value; break;
case 5: red = hsv.value; green = p; blue = u; break;
}
switch (i)
{
case 0: red = hsv.value; green = u; blue = p; break;
case 1: red = u; green = hsv.value; blue = p; break;
case 2: red = p; green = hsv.value; blue = u; break;
case 3: red = p; green = u; blue = hsv.value; break;
case 4: red = u; green = p; blue = hsv.value; break;
case 5: red = hsv.value; green = p; blue = u; break;
}
}
}

} // namespace modm::color

0 comments on commit 53afb65

Please sign in to comment.