Skip to content

Commit

Permalink
Removed const in constructors, UnderlyingType -> T
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Apr 18, 2021
1 parent a122503 commit 1688174
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 58 deletions.
29 changes: 13 additions & 16 deletions src/modm/ui/color/hsv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,46 @@ namespace modm
namespace color
{

template<typename UnderlyingType>
template<typename T>
class RgbT;

class Rgb565;
/// @ingroup modm_ui_color
template<typename UnderlyingType = uint8_t>
template<typename T = uint8_t>
class HsvT
{
public:
UnderlyingType hue{0};
UnderlyingType saturation{0};
UnderlyingType value{0};
T hue{0};
T saturation{0};
T value{0};

constexpr HsvT() = default;

constexpr HsvT(const UnderlyingType hue, const UnderlyingType saturation,
const UnderlyingType value)
: hue(hue), saturation(saturation), value(value)
{}
constexpr HsvT(T hue, T saturation, T value) : hue(hue), saturation(saturation), value(value) {}

template<typename T>
constexpr HsvT(const RgbT<T>& rgb);
template<typename _T>
constexpr HsvT(const RgbT<_T>& rgb);

constexpr HsvT(const Rgb565& rgb565) : HsvT(RgbT<uint8_t>(rgb565)) {}

constexpr bool
operator==(const HsvT<UnderlyingType>& other) const
operator==(const HsvT<T>& other) const
{
return (hue == other.hue and saturation == other.saturation and value == other.value);
}

private:
template<typename T>
template<typename _T>
friend IOStream&
operator<<(IOStream& os, const HsvT<T>&);
operator<<(IOStream& os, const HsvT<_T>&);
};

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

template<typename UnderlyingType>
template<typename _T>
IOStream&
operator<<(IOStream& os, const color::HsvT<UnderlyingType>& color);
operator<<(IOStream& os, const color::HsvT<_T>& color);

} // namespace color
} // namespace modm
Expand Down
10 changes: 5 additions & 5 deletions src/modm/ui/color/hsv_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
* @see http://de.wikipedia.org/wiki/HSV-Farbraum#Umrechnung_RGB_in_HSV.2FHSL
* @param rgb
*/
template<typename UnderlyingType>
template<typename T>
constexpr modm::color::HsvT<UnderlyingType>::HsvT(const modm::color::RgbT<T> &rgb)
template<typename _T>
constexpr modm::color::HsvT<T>::HsvT(const modm::color::RgbT<_T> &rgb)
{
using CalcType = float;
const CalcType maxValue = std::numeric_limits<UnderlyingType>::max();
const CalcType maxValue = std::numeric_limits<T>::max();
const CalcType _red = CalcType(rgb.red) / maxValue;
const CalcType _blue = CalcType(rgb.blue) / maxValue;
const CalcType _green = CalcType(rgb.green) / maxValue;
Expand Down Expand Up @@ -70,9 +70,9 @@ constexpr modm::color::HsvT<UnderlyingType>::HsvT(const modm::color::RgbT<T> &rg
saturation = _diff / _max * maxValue;
}

template<typename UnderlyingType>
template<typename _T>
modm::IOStream &
modm::color::operator<<(modm::IOStream &os, const modm::color::HsvT<UnderlyingType> &color)
modm::color::operator<<(modm::IOStream &os, const modm::color::HsvT<_T> &color)
{
os << color.hue << "\t" << color.saturation << "\t" << color.value;
return os;
Expand Down
44 changes: 22 additions & 22 deletions src/modm/ui/color/rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,68 +30,68 @@ namespace modm
namespace color
{

template<typename UnderlyingType>
template<typename T>
class HsvT;

class Rgb565;

/// @ingroup modm_ui_color
template<typename UnderlyingType = uint8_t>
template<typename T = uint8_t>
class RgbT
{
public:
UnderlyingType red{0};
UnderlyingType green{0};
UnderlyingType blue{0};
T red{0};
T green{0};
T blue{0};

constexpr RgbT() = default;

constexpr RgbT(const UnderlyingType red, const UnderlyingType green, const UnderlyingType blue)
: red(red), green(green), blue(blue)
{}
constexpr RgbT(T red, T green, T blue) : red(red), green(green), blue(blue) {}

template<typename T>
constexpr RgbT(const HsvT<T>& hsv);
template<typename _T>
constexpr RgbT(const HsvT<_T>& hsv);

constexpr RgbT(const Rgb565& rgb565)
: red((rgb565.color >> 8) & 0xF8), green((rgb565.color >> 3) & 0xFC), blue(rgb565.color << 3)
: red((rgb565.color >> 8) & 0xF8),
green((rgb565.color >> 3) & 0xFC),
blue(rgb565.color << 3)
{}

template<typename IntermediateType = float, unsigned int multiplier = 100,
typename ReturnType = UnderlyingType>
typename ReturnType = T>
ReturnType
getRelative(const UnderlyingType color) const
getRelative(const T color) const
{
return ReturnType(IntermediateType(color) * IntermediateType(multiplier) /
IntermediateType(red + green + blue));
}

template<typename IntermediateType = float, unsigned int multiplier = 100,
typename ReturnType = UnderlyingType>
typename ReturnType = T>
ReturnType
getRelativeRed() const
{
return getRelative<IntermediateType, multiplier, ReturnType>(red);
}

template<typename IntermediateType = float, unsigned int multiplier = 100,
typename ReturnType = UnderlyingType>
typename ReturnType = T>
ReturnType
getRelativeGreen() const
{
return getRelative<IntermediateType, multiplier, ReturnType>(green);
}

template<typename IntermediateType = float, unsigned int multiplier = 100,
typename ReturnType = UnderlyingType>
typename ReturnType = T>
ReturnType
getRelativeBlue() const
{
return getRelative<IntermediateType, multiplier, ReturnType>(blue);
}

template<typename IntermediateType = float, unsigned int multiplier = 100,
typename ReturnType = UnderlyingType>
typename ReturnType = T>
RgbT<ReturnType>
getRelativeColors() const
{
Expand All @@ -101,23 +101,23 @@ class RgbT
}

constexpr bool
operator==(const RgbT<UnderlyingType>& other) const
operator==(const RgbT<T>& other) const
{
return (red == other.red and green == other.green and blue == other.blue);
}

private:
template<typename T>
template<typename _T>
friend IOStream&
operator<<(IOStream& os, const RgbT<T>&);
operator<<(IOStream& os, const RgbT<_T>&);
};

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

template<typename UnderlyingType>
template<typename _T>
IOStream&
operator<<(IOStream& os, const color::RgbT<UnderlyingType>& color);
operator<<(IOStream& os, const color::RgbT<_T>& color);

} // namespace color

Expand Down
16 changes: 8 additions & 8 deletions src/modm/ui/color/rgb565.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace modm
namespace color
{

template<typename UnderlyingType>
template<typename T>
class RgbT;

template<typename UnderlyingType>
template<typename T>
class HsvT;

// Rgb565 (565) Format
Expand All @@ -35,18 +35,18 @@ class Rgb565

constexpr Rgb565() = default;

constexpr Rgb565(const uint16_t color) : color(color) {}
constexpr Rgb565(uint16_t color) : color(color) {}

constexpr Rgb565(const uint8_t red, const uint8_t green, const uint8_t blue)
constexpr Rgb565(uint8_t red, uint8_t green, uint8_t blue)
: color(uint16_t(red & 0xF8) << 8 | uint16_t(green & 0xFC) << 3 | uint16_t(blue >> 3))
{}

template<typename T>
constexpr Rgb565(const RgbT<T> &rgb) : Rgb565(rgb.red, rgb.green, rgb.blue)
template<typename _T>
constexpr Rgb565(const RgbT<_T> &rgb) : Rgb565(rgb.red, rgb.green, rgb.blue)
{}

template<typename T>
constexpr Rgb565(const HsvT<T> &hsv) : Rgb565(RgbT<uint8_t>(hsv))
template<typename _T>
constexpr Rgb565(const HsvT<_T> &hsv) : Rgb565(RgbT<uint8_t>(hsv))
{}

inline bool
Expand Down
14 changes: 7 additions & 7 deletions src/modm/ui/color/rgb_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
#include <type_traits>

// TODO Finnish generalisation for uint16_t
template<typename UnderlyingType>
template<typename T>
constexpr modm::color::RgbT<UnderlyingType>::RgbT(const modm::color::HsvT<T> &hsv)
template<typename _T>
constexpr modm::color::RgbT<T>::RgbT(const modm::color::HsvT<_T> &hsv)
{
uint16_t vs = hsv.value * hsv.saturation;
uint16_t h6 = 6 * hsv.hue;

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

switch (i)
{
Expand All @@ -45,9 +45,9 @@ constexpr modm::color::RgbT<UnderlyingType>::RgbT(const modm::color::HsvT<T> &hs
}
}

template<typename UnderlyingType>
template<typename _T>
modm::IOStream &
modm::color::operator<<(modm::IOStream &os, const modm::color::RgbT<UnderlyingType> &color)
modm::color::operator<<(modm::IOStream &os, const modm::color::RgbT<_T> &color)
{
os << color.red << "\t" << color.green << "\t" << color.blue;
return os;
Expand Down

0 comments on commit 1688174

Please sign in to comment.