Skip to content

Commit

Permalink
getRelative multiplier as runtime parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Apr 26, 2021
1 parent 6b59807 commit 81645e0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 96 deletions.
29 changes: 15 additions & 14 deletions src/modm/ui/color/brightness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#include <stdint.h>

#include <modm/io/iostream.hpp>
#include <concepts>
#include <modm/io/iostream.hpp>

#include "hsv.hpp"
#include "rgb.hpp"
Expand All @@ -36,11 +36,12 @@ class HsvT;
class Rgb565;

/**
* Brightness, used with grayscale and monochrome displays, dimmed leds, brightness sensors
* \brief Brightness as unsigned integral. Add's the math for conversion to and from
* Color-Types. Use with: Grayscale Buffers, Dimmed LEDs, Brightness sensors
*
* \author Thomas Sommer
*
* \tparam T Unsigned Int for storing the brightness
*
* \tparam T Unsigned integral for the brightness-value
* \ingroup modm_ui_color
*/

Expand All @@ -52,37 +53,37 @@ class BrightnessT

constexpr BrightnessT() = default;

constexpr BrightnessT(T value) : value(value)
{}
constexpr BrightnessT(T value) : value(value) {}

/**
* Convertion Constructor for RGB Color
*
* \param rgb RGB Color
*/
template<std::unsigned_integral _T>
constexpr BrightnessT(RgbT<_T> rgb)
: value((0.2125 * float(rgb.red)) + (0.7154 * float(rgb.green)) + (0.0721 * float(rgb.blue)))
template<std::unsigned_integral U>
constexpr BrightnessT(RgbT<U> rgb)
: value((0.2125 * float(rgb.red)) + (0.7154 * float(rgb.green)) +
(0.0721 * float(rgb.blue)))
{}

/**
* Convertion Constructor for HSV Color
*
* \param hsv HSV Color
*/
template<std::unsigned_integral _T>
constexpr BrightnessT(HsvT<_T> hsv) : value(hsv.value)
template<std::unsigned_integral U>
constexpr BrightnessT(HsvT<U> hsv) : value(hsv.value)
{}

/**
* Convertion Constructor for RGB565 Color
*
* \param rgb565 RGB565 Color
*/
constexpr BrightnessT(Rgb565 rgb565) : BrightnessT<T>(RgbT<uint8_t>(rgb565))
{}
constexpr BrightnessT(Rgb565 rgb565) : BrightnessT<T>(RgbT<uint8_t>(rgb565)) {}

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

/// @ingroup modm_ui_color
Expand Down
22 changes: 11 additions & 11 deletions src/modm/ui/color/hsv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class BrightnessT;
class Rgb565;

/**
* Color in HSV Colorspace
* \brief Color in HSV Colorspace
*
* @author Martin Rosekeit, Fabian Greif, Niklas Hauser, David Hebbeker, Thomas Sommer
* @ingroup modm_ui_color
* \author Martin Rosekeit, Fabian Greif, Niklas Hauser, David Hebbeker, Thomas Sommer
* \ingroup modm_ui_color
*/

// TODO conecpt to only allow unsigned int or float
Expand All @@ -64,16 +64,16 @@ class HsvT
*
* \param rgb RGB Color
*/
template<std::unsigned_integral _T>
constexpr HsvT(const RgbT<_T>& rgb);
template<std::unsigned_integral U>
constexpr HsvT(const RgbT<U>& rgb);

/**
* Convertion Constructor for Brightness
*
* \param brightness Brightness 'Color'-object
*/
template<std::unsigned_integral _T>
constexpr HsvT(const BrightnessT<_T> gray) : hue(0), saturation(0), value(gray.value)
template<std::unsigned_integral U>
constexpr HsvT(const BrightnessT<U> gray) : hue(0), saturation(0), value(gray.value)
{}

/**
Expand All @@ -87,17 +87,17 @@ class HsvT
operator==(const HsvT<T>& other) const = default;

private:
template<std::unsigned_integral _T>
template<std::unsigned_integral U>
friend IOStream&
operator<<(IOStream& os, const HsvT<_T>&);
operator<<(IOStream& os, const HsvT<U>&);
};

/// @ingroup modm_ui_color
using Hsv = HsvT<>;

template<std::unsigned_integral _T>
template<std::unsigned_integral U>
IOStream&
operator<<(IOStream& os, const color::HsvT<_T>& color);
operator<<(IOStream& os, const color::HsvT<U>& color);

} // namespace color
} // namespace modm
Expand Down
8 changes: 4 additions & 4 deletions src/modm/ui/color/hsv_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* @param rgb
*/
template<std::unsigned_integral T>
template<std::unsigned_integral _T>
constexpr modm::color::HsvT<T>::HsvT(const modm::color::RgbT<_T> &rgb)
template<std::unsigned_integral U>
constexpr modm::color::HsvT<T>::HsvT(const modm::color::RgbT<U> &rgb)
{
using CalcType = float;
const CalcType maxValue = std::numeric_limits<T>::max();
Expand Down Expand Up @@ -70,9 +70,9 @@ constexpr modm::color::HsvT<T>::HsvT(const modm::color::RgbT<_T> &rgb)
saturation = _diff / _max * maxValue;
}

template<std::unsigned_integral _T>
template<std::unsigned_integral U>
modm::IOStream &
modm::color::operator<<(modm::IOStream &os, const modm::color::HsvT<_T> &color)
modm::color::operator<<(modm::IOStream &os, const modm::color::HsvT<U> &color)
{
os << color.hue << "\t" << color.saturation << "\t" << color.value;
return os;
Expand Down
96 changes: 45 additions & 51 deletions src/modm/ui/color/rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class BrightnessT;
class Rgb565;

/**
* Color in HSV Colorspace
* \brief Color in HSV Colorspace
*
* @author Martin Rosekeit, Fabian Greif, Niklas Hauser, David Hebbeker, Thomas Sommer
* @ingroup modm_ui_color
* \author Martin Rosekeit, Fabian Greif, Niklas Hauser, David Hebbeker, Thomas Sommer
* \ingroup modm_ui_color
*/

template<std::unsigned_integral T = uint8_t>
Expand All @@ -70,17 +70,17 @@ class RgbT
*
* \param hsv HSV Color
*/
template<std::unsigned_integral _T>
constexpr RgbT(const HsvT<_T>& hsv);
template<std::unsigned_integral U>
constexpr RgbT(const HsvT<U>& hsv);

/**
* Convertion Constructor for Brightness
*
* \param brightness Brightness 'Color'-object
*/
// TODO Plump conversion, implement the right way
template<std::unsigned_integral _T>
constexpr RgbT(const BrightnessT<_T> brightness)
template<std::unsigned_integral U>
constexpr RgbT(const BrightnessT<U> brightness)
: red(brightness), green(brightness), blue(brightness)
{}

Expand All @@ -101,13 +101,41 @@ class RgbT
return red + green + blue;
}

template<modm::fundamental IntermediateType = float, unsigned int multiplier = 100,
std::unsigned_integral ReturnType = T>
template<modm::arithmetic IntermediateType = float, std::unsigned_integral ReturnType = T>
inline ReturnType
getRelative(const T color, IntermediateType multiplier = 100) const
{
return IntermediateType(color) * multiplier / IntermediateType(getColorSum());
}

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

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

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

template<modm::arithmetic IntermediateType = float, std::unsigned_integral ReturnType = T>
RgbT<ReturnType>
getRelativeColors(IntermediateType multiplier = 100) const
{
return IntermediateType(color) * IntermediateType(multiplier) /
IntermediateType(getColorSum());
return RgbT<ReturnType>(getRelativeRed<IntermediateType, ReturnType>(multiplier),
getRelativeGreen<IntermediateType, ReturnType>(multiplier),
getRelativeBlue<IntermediateType, ReturnType>(multiplier));
}

/**
Expand All @@ -120,7 +148,7 @@ class RgbT
* In order to still have some signal the very low green value can be
* amplified with the clear value.
*/
template<modm::fundamental IntermediateType = float>
template<modm::arithmetic IntermediateType = float>
void
normalizeByValue(T value)
{
Expand All @@ -130,40 +158,6 @@ class RgbT
blue *= scalar;
}

template<modm::fundamental IntermediateType = float, unsigned int multiplier = 100,
std::unsigned_integral ReturnType = T>
ReturnType
getRelativeRed() const
{
return getRelative<IntermediateType, multiplier, ReturnType>(red);
}

template<modm::fundamental IntermediateType = float, unsigned int multiplier = 100,
std::unsigned_integral ReturnType = T>
ReturnType
getRelativeGreen() const
{
return getRelative<IntermediateType, multiplier, ReturnType>(green);
}

template<modm::fundamental IntermediateType = float, unsigned int multiplier = 100,
std::unsigned_integral ReturnType = T>
ReturnType
getRelativeBlue() const
{
return getRelative<IntermediateType, multiplier, ReturnType>(blue);
}

template<modm::fundamental IntermediateType = float, unsigned int multiplier = 100,
std::unsigned_integral ReturnType = T>
RgbT<ReturnType>
getRelativeColors() const
{
return RgbT<ReturnType>(getRelativeRed<IntermediateType, multiplier, ReturnType>(),
getRelativeGreen<IntermediateType, multiplier, ReturnType>(),
getRelativeBlue<IntermediateType, multiplier, ReturnType>());
}

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

Expand Down Expand Up @@ -262,17 +256,17 @@ class RgbT
}

private:
template<std::unsigned_integral _T>
template<std::unsigned_integral U>
friend IOStream&
operator<<(IOStream& os, const RgbT<_T>&);
operator<<(IOStream& os, const RgbT<U>&);
};

/// @ingroup modm_ui_color
using Rgb = RgbT<>;

template<std::unsigned_integral _T>
template<std::unsigned_integral U>
IOStream&
operator<<(IOStream& os, const color::RgbT<_T>& color);
operator<<(IOStream& os, const color::RgbT<U>& color);

} // namespace color

Expand Down
18 changes: 9 additions & 9 deletions src/modm/ui/color/rgb565.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ template<std::unsigned_integral T>
class BrightnessT;

/**
* Color in RGB Colorspace, 16 bits: RRRR RGGG GGGB BBBB
* \brief Color in RGB Colorspace, 16 bits: RRRR RGGG GGGB BBBB
*
* @author Fabian Greif, Thomas Sommer
* @ingroup modm_ui_color
* \author Fabian Greif, Thomas Sommer
* \ingroup modm_ui_color
*/

class Rgb565
Expand Down Expand Up @@ -72,26 +72,26 @@ class Rgb565
*
* \param rgb RGB Color
*/
template<std::unsigned_integral _T>
constexpr Rgb565(const RgbT<_T> &rgb) : Rgb565(rgb.red, rgb.green, rgb.blue)
template<std::unsigned_integral U>
constexpr Rgb565(const RgbT<U> &rgb) : Rgb565(rgb.red, rgb.green, rgb.blue)
{}

/**
* Convertion Constructor for HSV Color
*
* \param hsv HSV Color
*/
template<std::unsigned_integral _T>
constexpr Rgb565(const HsvT<_T> &hsv) : Rgb565(RgbCalcType(hsv))
template<std::unsigned_integral U>
constexpr Rgb565(const HsvT<U> &hsv) : Rgb565(RgbCalcType(hsv))
{}

/**
* Convertion Constructor for Brightness
*
* \param brightness Brightness 'Color'-object
*/
template<std::unsigned_integral _T>
constexpr Rgb565(const BrightnessT<_T> brightness) : Rgb565(RgbCalcType(brightness))
template<std::unsigned_integral U>
constexpr Rgb565(const BrightnessT<U> brightness) : Rgb565(RgbCalcType(brightness))
{}

constexpr bool
Expand Down
8 changes: 4 additions & 4 deletions src/modm/ui/color/rgb_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

// TODO Finnish generalisation for uint16_t
template<std::unsigned_integral T>
template<std::unsigned_integral _T>
constexpr modm::color::RgbT<T>::RgbT(const modm::color::HsvT<_T> &hsv)
template<std::unsigned_integral U>
constexpr modm::color::RgbT<T>::RgbT(const modm::color::HsvT<U> &hsv)
{
uint16_t vs = hsv.value * hsv.saturation;
uint16_t h6 = 6 * hsv.hue;
Expand All @@ -44,9 +44,9 @@ constexpr modm::color::RgbT<T>::RgbT(const modm::color::HsvT<_T> &hsv)
}
}

template<std::unsigned_integral _T>
template<std::unsigned_integral U>
modm::IOStream &
modm::color::operator<<(modm::IOStream &os, const modm::color::RgbT<_T> &color)
modm::color::operator<<(modm::IOStream &os, const modm::color::RgbT<U> &color)
{
os << color.red << "\t" << color.green << "\t" << color.blue;
return os;
Expand Down
7 changes: 4 additions & 3 deletions src/modm/ui/color/rgbhtml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ namespace color

namespace html {
/**
* HTML Colornames
* \brief Constant HTML Colornames in RGB Colorspace
*
* \ingroup modm_ui_color
* \see https://htmlcolorcodes.com/color-names/
* \see https://htmlcolorcodes.com/color-names/
* \see modm:color:RgbT
* \ingroup modm_ui_color
*/

// Red HTML Color Names
Expand Down

0 comments on commit 81645e0

Please sign in to comment.