Skip to content

Commit d7a3522

Browse files
committed
very generic modm:ui:color
1 parent 3d2a6c0 commit d7a3522

19 files changed

+1296
-889
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2021, Thomas Sommer
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
#include <algorithm>
15+
16+
#include <modm/math/utils/integer_traits.hpp>
17+
#include <modm/architecture/interface/assert.hpp>
18+
19+
namespace modm {
20+
21+
/**
22+
* @brief Unsigned integer with arbitrary digits and scaling value on conversion
23+
* between instances with different digits.
24+
*
25+
* @tparam D Number of Digits
26+
*
27+
* @author Thomas Sommer
28+
* @ingroup modm_math
29+
*/
30+
template<int D>
31+
requires (D > 0)
32+
class ProportionalUnsigned {
33+
public:
34+
static constexpr int Digits = D;
35+
36+
using T = uint_t<D>::least;
37+
static constexpr T min = 0;
38+
static constexpr T max = bitmask<D>();
39+
40+
constexpr ProportionalUnsigned() = default;
41+
42+
constexpr ProportionalUnsigned(T value) {
43+
// TODO not sure if modm_assert_continue_fail_debug or modm_assert_continue_fail is needed
44+
45+
// FIXME with AVR-GCC we get:
46+
// error: '__c' declared 'static' in 'constexpr' function
47+
// modm_assert_continue_fail(value <= max, "ProportionalUnsigned", "value out of range");
48+
this->value = value;
49+
}
50+
51+
// Construct from bigger or equal ProportionalUnsigned
52+
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
53+
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other)
54+
: value(other.value >> (E - D)) {}
55+
56+
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
57+
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other)
58+
: value(other.value >> (E - D)) {}
59+
60+
// Construct from smaller ProportionalUnsigned
61+
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
62+
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other)
63+
: value(other.value * max / other.max)
64+
{}
65+
66+
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
67+
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other)
68+
: value(other.value * max / other.max)
69+
{}
70+
71+
/* // Faster construction for D == 1
72+
constexpr ProportionalUnsigned(const ProportionalUnsigned<1> &other) : value(other.value ? bitmask<D>() : 0){}
73+
74+
// constexpr ProportionalUnsigned(ProportionalUnsigned<1> &&other) : value(other.value ? bitmask<D>() : 0){}
75+
constexpr ProportionalUnsigned& operator=(const ProportionalUnsigned<1> &other) {
76+
value = other.value ? bitmask<D>() : 0;
77+
return *this;
78+
} */
79+
80+
T getValue() const
81+
{ return value; }
82+
83+
// Cast to underlying type. No more comparison operators required.
84+
// @see https://en.cppreference.com/w/cpp/language/cast_operator
85+
operator T() const
86+
{ return value; }
87+
88+
void operator=(T value) {
89+
// TODO not sure if modm_assert_continue_fail_debug or modm_assert_continue_fail is needed
90+
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range");
91+
this->value = value;
92+
}
93+
94+
// Assign ProportionalUnsigned with more or equal Digits
95+
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
96+
constexpr void operator=(const ProportionalUnsigned<E>& other) {
97+
value = other.value >> (E - D);
98+
}
99+
100+
// Assign ProportionalUnsigned with less Digits
101+
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
102+
constexpr void operator=(const ProportionalUnsigned<E>& other) {
103+
value = other.value * max / other.max;
104+
}
105+
protected:
106+
T value{0};
107+
108+
private:
109+
template<int>
110+
friend class ProportionalUnsigned;
111+
};
112+
113+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021, Thomas Sommer
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
def init(module):
14+
module.name = ":math:proportional_unsigned"
15+
module.description = """
16+
# Proportional Unsigned
17+
18+
Unsigned integer with arbitrary digits D and proportional scaling between
19+
instances with different digits. F.e. a ProportionalUnsigned<2> with the value
20+
0b11 converted to a to a ProportionalUnsigned<4> becomes the value 0b1111.
21+
22+
It's the baseclass to color::Gray and thus to any other colors too but may have more applications.
23+
"""
24+
25+
def prepare(module, options):
26+
module.depends(
27+
":utils",
28+
":architecture:assert"
29+
)
30+
return True
31+
32+
def build(env):
33+
env.outbasepath = "modm/src/modm/math"
34+
env.copy("proportional_unsigned.hpp")

src/modm/ui/color.cpp

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/modm/ui/color.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
/*
2-
* Copyright (c) 2009, Martin Rosekeit
3-
* Copyright (c) 2009-2013, Fabian Greif
4-
* Copyright (c) 2012-2013, 2015, Niklas Hauser
5-
* Copyright (c) 2013, David Hebbeker
62
* Copyright (c) 2021, Thomas Sommer
73
*
84
* This file is part of the modm project.
@@ -12,10 +8,11 @@
128
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
139
*/
1410
// ----------------------------------------------------------------------------
11+
#pragma once
1512

13+
#include "color/gray.hpp"
1614
#include "color/rgb.hpp"
1715
#include "color/hsv.hpp"
18-
#include "color/brightness.hpp"
1916

20-
#include "color/rgb565.hpp"
21-
#include "color/rgbhtml.hpp"
17+
#include "color/rgb_html.hpp"
18+
#include "color/rgb_stacked.hpp"

src/modm/ui/color/brightness.hpp

Lines changed: 0 additions & 105 deletions
This file was deleted.

src/modm/ui/color/color.lb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# Copyright (c) 2018, Niklas Hauser
4+
# Copyright (c) 2021, Thomas Sommer
55
#
66
# This file is part of the modm project.
77
#
@@ -18,11 +18,20 @@ def init(module):
1818
Color containers and converters in various formats: RGB, HSV, Brightness, Rgb565
1919
"""
2020

21+
2122
def prepare(module, options):
22-
module.depends(":math:utils")
23+
module.depends(
24+
":math:utils",
25+
":math:proportional_unsigned",
26+
":math:saturation"
27+
)
2328
return True
2429

30+
2531
def build(env):
2632
env.outbasepath = "modm/src/modm/ui/color"
27-
env.copy(".")
33+
34+
ignore = ["*pattern*"]
35+
env.copy(".", ignore=env.ignore_paths(*ignore))
36+
2837
env.copy("../color.hpp")

0 commit comments

Comments
 (0)