Skip to content

Commit

Permalink
Fix instability of hsv color around red
Browse files Browse the repository at this point in the history
  • Loading branch information
malcx95 committed Feb 24, 2024
1 parent 60e0fae commit ff676c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
11 changes: 9 additions & 2 deletions core/backlight/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <stdint.h>
#include <cmath>
#include "../util/math_utils.h"


namespace core::backlight
{
Expand Down Expand Up @@ -49,8 +51,13 @@ struct Color
*/
static Color from_hsv(float hue, float saturation, float value)
{
// This function was AI-generated
const float h = hue * 6.0f;
// Most of this function was AI-generated

// we need to clamp the hue as it otherwise manifests itself as a numerical
// instability around red.
const float hue_clamped = core::util::clamp(hue, 0.0001f, 0.9999f);

const float h = hue_clamped * 6.0f;
const float f = h - floor(h);
const float p = value * (1.0f - saturation);
const float q = value * (1.0f - saturation * f);
Expand Down
17 changes: 17 additions & 0 deletions core/util/math_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <cstdint>
#include <cmath>
#include <type_traits>

namespace core::util
{

template<typename T>
T clamp(T value, T min, T max)
{
static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
return std::min(std::max(value, min), max);
}

}
3 changes: 2 additions & 1 deletion docs/serialization_format.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# General

- Everything is `uint16_t`
- Everything is 0 indexed

## Action

Action:
```
[N, key_1, modifier_1, media_1, ..., key_N, modifier_N, media_N]
```
where `N` is the sequence length.
where `N` is the sequence length. A blank key is 0xF000, a blank modifier is 0xE000 and a blank media key is 0xE400.

## KeyMap

Expand Down

0 comments on commit ff676c0

Please sign in to comment.