Skip to content

Commit f0b7dd1

Browse files
committed
very generic modm:ui:color
1 parent 2565136 commit f0b7dd1

19 files changed

+1185
-886
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
if(std::is_constant_evaluated())
44+
this->value = std::min(value, max);
45+
else {
46+
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range");
47+
// Constructing runtime should a.f.a.p.
48+
this->value = value;
49+
}
50+
}
51+
52+
// Construct from bigger or equal ProportionalUnsigned
53+
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
54+
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other)
55+
: value(other.value >> (E - D)) {}
56+
57+
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
58+
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other)
59+
: value(other.value >> (E - D)) {}
60+
61+
// Construct from smaller ProportionalUnsigned
62+
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
63+
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other)
64+
: value(other.value * max / other.max)
65+
{}
66+
67+
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
68+
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other)
69+
: value(other.value * max / other.max)
70+
{}
71+
72+
/* // Faster construction for D == 1
73+
constexpr ProportionalUnsigned(const ProportionalUnsigned<1> &other) : value(other.value ? bitmask<D>() : 0){}
74+
75+
// constexpr ProportionalUnsigned(ProportionalUnsigned<1> &&other) : value(other.value ? bitmask<D>() : 0){}
76+
constexpr ProportionalUnsigned& operator=(const ProportionalUnsigned<1> &other) {
77+
value = other.value ? bitmask<D>() : 0;
78+
return *this;
79+
} */
80+
81+
// Cast to underlying type. No need to define getters and comparison operators.
82+
// @see https://en.cppreference.com/w/cpp/language/cast_operator
83+
operator T() const
84+
{ return value; }
85+
86+
// Assign ProportionalUnsigned with more or equal Digits
87+
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
88+
constexpr void operator=(const ProportionalUnsigned<E>& other) {
89+
value = other.value >> (E - D);
90+
}
91+
92+
// Assign ProportionalUnsigned with less Digits
93+
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
94+
constexpr void operator=(const ProportionalUnsigned<E>& other) {
95+
value = other.value * max / other.max;
96+
}
97+
98+
constexpr void setValue(T value) {
99+
if(std::is_constant_evaluated())
100+
this->value = std::min(value, max);
101+
else {
102+
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range");
103+
// Calling setValue() runtime should a.f.a.p.
104+
this->value = value;
105+
}
106+
}
107+
108+
protected:
109+
T value{0};
110+
111+
private:
112+
template<int>
113+
friend class ProportionalUnsigned;
114+
};
115+
116+
}
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)