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 Nov 15, 2021
1 parent f37c731 commit 984e341
Show file tree
Hide file tree
Showing 19 changed files with 1,114 additions and 801 deletions.
115 changes: 115 additions & 0 deletions src/modm/math/scaling_unsigned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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 Digits
*
* @author Thomas Sommer
*/
template<int D>
class ScalingUnsigned {
public:
static constexpr int Digits = D;
using ValueType = uint_t<D>::least;

static constexpr ValueType min = 0;
static constexpr ValueType max = bitmask<D>();

constexpr ScalingUnsigned() = default;

// FIXME want both:
// COMPTIME: static_assert(value <= max, "value out of range")
// RUNTIME: std::min(value, max) or modm_assert(value <= max, ...)
constexpr ScalingUnsigned(ValueType value) : value(std::min(value, max)) {
// TODO disable via lbuild option
// modm_assert_continue_fail_debug(value <= max, "ScalingUnsigned", "constructor", "value out of range");
}

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

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

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

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

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

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

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

// FIXME want both:
// COMPTIME: static_assert(value <= max, "value out of range")
// RUNTIME: std::min(value, max) or modm_assert(value <= max, ...)
void setValue(ValueType value) {
this->value = std::min(value, max);
// TODO disable via lbuild option
// modm_assert_continue_fail_debug(value <= max, "modm::ScalingUnsigned", "setValue()", "value out of range");
}

ValueType getValue() const { return value; }

bool isSaturated() const {
return value == max;
}

constexpr auto operator<=>(const ScalingUnsigned<D> &) const = default;

protected:
ValueType value = 0;

inline void max_cutoff() { value = std::min(value, max); }
private:
template<int>
friend class ScalingUnsigned;
};

}
32 changes: 32 additions & 0 deletions src/modm/math/scaling_unsigned.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/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:scaling_unsigned"
module.description = """
# Scaling Unsigned
Unsigned integer with arbitrary digits and scaling value on conversion between
instances with different digits. F.e. a 2bit scaling unsigned of 0b11 becomes
0b1111 when converted to a 4bit scaling unsigned.
It's the baseclass to all in modm::color::* but may have wider applications.
"""

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

def build(env):
env.outbasepath = "modm/src/modm/math"
env.copy("scaling_unsigned.hpp")
10 changes: 10 additions & 0 deletions src/modm/math/utils/integer_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ namespace modm

template <typename ... Ts>
using fits_any_t = typename fits_any<Ts...>::type;

template<unsigned int N>
struct bitmask {
using value_type = uint_t<N>::least;
static constexpr value_type value = std::pow(2, N) - 1;
constexpr operator value_type() const noexcept { return value; }
};

template<unsigned int N>
using bitmask_t = typename bitmask<N>::value_type;
}
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:scaling_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 984e341

Please sign in to comment.