Skip to content

Commit

Permalink
very generic modm:ui:color
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Jan 11, 2022
1 parent 2565136 commit 39412c6
Show file tree
Hide file tree
Showing 19 changed files with 1,186 additions and 887 deletions.
116 changes: 116 additions & 0 deletions src/modm/math/proportional_unsigned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2021, Thomas Sommer
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include <algorithm>

#include <modm/math/utils/integer_traits.hpp>
#include <modm/architecture/interface/assert.hpp>

namespace modm {

/**
* @brief Unsigned integer with arbitrary digits and scaling value on conversion
* between instances with different digits.
*
* @tparam D Number of Digits
*
* @author Thomas Sommer
* @ingroup modm_math
*/
template<int D>
requires (D > 0)
class ProportionalUnsigned {
public:
static constexpr int Digits = D;

using T = uint_t<D>::least;
static constexpr T min = 0;
static constexpr T max = bitmask<D>();

constexpr ProportionalUnsigned() = default;

constexpr ProportionalUnsigned(T value) {
if(std::is_constant_evaluated())
this->value = std::min(value, max);
else {
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range");
// Constructing runtime should a.f.a.p.
this->value = value;
}
}

// Construct from bigger or equal ProportionalUnsigned
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other)
: value(other.value >> (E - D)) {}

template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other)
: value(other.value >> (E - D)) {}

// Construct from smaller ProportionalUnsigned
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other)
: value(other.value * max / other.max)
{}

template <int E, std::enable_if_t<(D > E), void*> = nullptr>
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other)
: value(other.value * max / other.max)
{}

/* // Faster construction for D == 1
constexpr ProportionalUnsigned(const ProportionalUnsigned<1> &other) : value(other.value ? bitmask<D>() : 0){}
// constexpr ProportionalUnsigned(ProportionalUnsigned<1> &&other) : value(other.value ? bitmask<D>() : 0){}
constexpr ProportionalUnsigned& operator=(const ProportionalUnsigned<1> &other) {
value = other.value ? bitmask<D>() : 0;
return *this;
} */

// Cast to underlying type. No need to define getters and comparison operators.
// @see https://en.cppreference.com/w/cpp/language/cast_operator
operator T() const
{ return value; }

// Assign ProportionalUnsigned with more or equal Digits
template <int E, std::enable_if_t<(D <= E), void*> = nullptr>
constexpr void operator=(const ProportionalUnsigned<E>& other) {
value = other.value >> (E - D);
}

// Assign ProportionalUnsigned with less Digits
template <int E, std::enable_if_t<(D > E), void*> = nullptr>
constexpr void operator=(const ProportionalUnsigned<E>& other) {
value = other.value * max / other.max;
}

constexpr void setValue(T value) {
if(std::is_constant_evaluated())
this->value = std::min(value, max);
else {
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range");
// Calling setValue() runtime should a.f.a.p.
this->value = value;
}
}

protected:
T value{0};

private:
template<int>
friend class ProportionalUnsigned;
};

}
34 changes: 34 additions & 0 deletions src/modm/math/proportional_unsigned.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Thomas Sommer
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
module.name = ":math:proportional_unsigned"
module.description = """
# Proportional Unsigned
Unsigned integer with arbitrary digits D and proportional scaling between
instances with different digits. F.e. a ProportionalUnsigned<2> with the value
0b11 converted to a to a ProportionalUnsigned<4> becomes the value 0b1111.
It's the baseclass to color::Gray and thus to any other colors too but may have more applications.
"""

def prepare(module, options):
module.depends(
":utils",
":architecture:assert"
)
return True

def build(env):
env.outbasepath = "modm/src/modm/math"
env.copy("proportional_unsigned.hpp")
53 changes: 0 additions & 53 deletions src/modm/ui/color.cpp

This file was deleted.

11 changes: 4 additions & 7 deletions src/modm/ui/color.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/*
* Copyright (c) 2009, Martin Rosekeit
* Copyright (c) 2009-2013, Fabian Greif
* Copyright (c) 2012-2013, 2015, Niklas Hauser
* Copyright (c) 2013, David Hebbeker
* Copyright (c) 2021, Thomas Sommer
*
* This file is part of the modm project.
Expand All @@ -12,10 +8,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------
#pragma once

#include "color/gray.hpp"
#include "color/rgb.hpp"
#include "color/hsv.hpp"
#include "color/brightness.hpp"

#include "color/rgb565.hpp"
#include "color/rgbhtml.hpp"
#include "color/rgb_html.hpp"
#include "color/rgb_stacked.hpp"
105 changes: 0 additions & 105 deletions src/modm/ui/color/brightness.hpp

This file was deleted.

15 changes: 12 additions & 3 deletions src/modm/ui/color/color.lb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, Niklas Hauser
# Copyright (c) 2021, Thomas Sommer
#
# This file is part of the modm project.
#
Expand All @@ -18,11 +18,20 @@ def init(module):
Color containers and converters in various formats: RGB, HSV, Brightness, Rgb565
"""


def prepare(module, options):
module.depends(":math:utils")
module.depends(
":math:utils",
":math:proportional_unsigned",
":math:saturation"
)
return True


def build(env):
env.outbasepath = "modm/src/modm/ui/color"
env.copy(".")

ignore = ["*pattern*"]
env.copy(".", ignore=env.ignore_paths(*ignore))

env.copy("../color.hpp")
Loading

0 comments on commit 39412c6

Please sign in to comment.