mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.6 map2colour
This commit is contained in:
parent
2564e8e217
commit
6b7ab84902
@ -6,7 +6,7 @@ jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
|
@ -8,7 +8,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
|
@ -10,7 +10,7 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
|
@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.1.6] - 2023-02-21
|
||||
- update readme.md
|
||||
- update GitHub actions
|
||||
- update license 2023
|
||||
- minor edits
|
||||
|
||||
|
||||
## [0.1.5] - 2022-10-19
|
||||
- redo **begin()** of map2colourFast to always create divFactors.
|
||||
- add compare example to compare map2colour and map2colourFast.
|
||||
@ -22,9 +29,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
- improve performance map2RGB()
|
||||
|
||||
## [0.1.2] - 2021-12-06
|
||||
- add map2_565(),
|
||||
- add map2_565()
|
||||
- add + improve examples.
|
||||
- fix value > upper bug
|
||||
- fix value > upper bug.
|
||||
|
||||
## [0.1.1] - 2021-12-05
|
||||
- add user defined colour-map.
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2022 Rob Tillaart
|
||||
Copyright (c) 2021-2023 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -50,6 +50,12 @@ If returned false the code might behave in unexpected ways.
|
||||
|
||||
## Interface
|
||||
|
||||
```cpp
|
||||
#include "map2colour.h"
|
||||
```
|
||||
|
||||
#### Functions
|
||||
|
||||
- **map2colour()** constructor.
|
||||
- **map2colourFast()** constructor, (larger code base, more RAM and faster)
|
||||
- **bool begin(float \* values, uint32_t \* colourMap = NULL)** load the array with **7**
|
||||
@ -59,11 +65,13 @@ If the colour array is not given the last given (or the default) colour array is
|
||||
The function returns false if the array of values is not in increasing order.
|
||||
- **uint32_t map2RGB(float value)** returns RGB colour packed in an uint32_t **0x00RRGGBB**.
|
||||
If the value is out of range of the original values array, the value is always mapped upon the first colour.
|
||||
- **uint16_t map2_565(float value)** often used 16 bit colour format. Currently a wrapper around **map2RGB**.
|
||||
- **uint16_t map2_565(float value)** often used 16 bit colour format.
|
||||
Currently a wrapper around **map2RGB**.
|
||||
|
||||
Note: the arrays passed to **begin()** should both have at least 7 elements!
|
||||
|
||||
The colour array can be filled with decimal or HEX values or predefined colours can be used. See below.
|
||||
The colour array can be filled with decimal or HEX values or predefined colours can be used.
|
||||
See below.
|
||||
|
||||
|
||||
## Predefined colours
|
||||
@ -71,24 +79,16 @@ The colour array can be filled with decimal or HEX values or predefined colours
|
||||
Colours are represented as 24 bit RGB values and have the pattern **0x00RRGGBB**.
|
||||
|
||||
|
||||
| define | value |
|
||||
|:------------|:----------:|
|
||||
| M2C_BLACK | 0x00000000 |
|
||||
| M2C_SILVER | 0x00C0C0C0 |
|
||||
| M2C_GRAY | 0x00808080 |
|
||||
| M2C_WHITE | 0x00FFFFFF |
|
||||
| M2C_MAROON | 0x00800000 |
|
||||
| M2C_RED | 0x00FF0000 |
|
||||
| M2C_PURPLE | 0x00800080 |
|
||||
| M2C_FUCHSIA | 0x00FF00FF |
|
||||
| M2C_GREEN | 0x00008000 |
|
||||
| M2C_LIME | 0x000FF000 |
|
||||
| M2C_OLIVE | 0x00808000 |
|
||||
| M2C_YELLOW | 0x00FFFF00 |
|
||||
| M2C_NAVY | 0x00000080 |
|
||||
| M2C_BLUE | 0x000000FF |
|
||||
| M2C_TEAL | 0x00008080 |
|
||||
| M2C_AQUA | 0x0000FFFF |
|
||||
| define | value | define | value |
|
||||
|:--------------|:------------:|:-------------|:------------:|
|
||||
| M2C_BLACK | 0x00000000 | M2C_GREEN | 0x00008000 |
|
||||
| M2C_SILVER | 0x00C0C0C0 | M2C_LIME | 0x000FF000 |
|
||||
| M2C_GRAY | 0x00808080 | M2C_OLIVE | 0x00808000 |
|
||||
| M2C_WHITE | 0x00FFFFFF | M2C_YELLOW | 0x00FFFF00 |
|
||||
| M2C_MAROON | 0x00800000 | M2C_NAVY | 0x00000080 |
|
||||
| M2C_RED | 0x00FF0000 | M2C_BLUE | 0x000000FF |
|
||||
| M2C_PURPLE | 0x00800080 | M2C_TEAL | 0x00008080 |
|
||||
| M2C_FUCHSIA | 0x00FF00FF | M2C_AQUA | 0x0000FFFF |
|
||||
|
||||
|
||||
More colour definitions can be found on the internet
|
||||
@ -104,17 +104,19 @@ The minimum to implement is an intensity effect going from black towards a colou
|
||||
More complex colour schemes are possible, up to 7 different colours.
|
||||
This number 7 is hardcoded (for now) and that might change in the future.
|
||||
|
||||
|
||||
#### Experimental in 0.1.5
|
||||
|
||||
(was planned for 0.2.0)
|
||||
If you create a non-decreasing array of values one can create a break in the gradient. See example.
|
||||
If you create a non-decreasing array of values one can create a break in the colour gradient.
|
||||
See example.
|
||||
|
||||
```cpp
|
||||
float values[7] = { -200, -90, 0, 45, 45, 150, 180 };
|
||||
uint32_t colours[7] = { M2C_BLUE, M2C_AQUA, M2C_LIME, M2C_YELLOW, M2C_RED, M2C_YELLOW, M2C_BLUE};
|
||||
```
|
||||
|
||||
with the double 45 in the values array there would be no gradient between the **M2C_YELLOW** and **M2C_RED**
|
||||
With the double 45 in the values array there would be no gradient between the **M2C_YELLOW** and **M2C_RED**
|
||||
effectively having 2 continuous gradients.
|
||||
|
||||
Note: **begin()** will report such array as false.
|
||||
@ -125,38 +127,37 @@ Note: **begin()** will report such array as false.
|
||||
Indicative performance figures measured with performance example.
|
||||
Performance depends on colours chosen, platform etc.
|
||||
|
||||
Note: time in microseconds
|
||||
Note: UNO at 16 MHz, ESP32 at 240 MHz
|
||||
|
||||
#### version 0.1.2
|
||||
|
||||
| function call | time us UNO | time us ESP32 |
|
||||
|:-----------------------|------------:|--------------:|
|
||||
| begin(values) | 4 | 4 |
|
||||
| begin(values, colours) | 12 | 4 |
|
||||
| map2RGB(value) | 124 - 152 | 2 - 4 |
|
||||
| map2_565(value) | 124 - 168 | 2 - 4 |
|
||||
| function call | time UNO | time ESP32 |
|
||||
|:-------------------------|------------:|-------------:|
|
||||
| begin(values) | 4 | 4 |
|
||||
| begin(values, colours) | 12 | 4 |
|
||||
| map2RGB(value) | 124 - 152 | 2 - 4 |
|
||||
| map2_565(value) | 124 - 168 | 2 - 4 |
|
||||
|
||||
|
||||
#### version 0.1.3
|
||||
|
||||
| function call | time us UNO | time us ESP32 |
|
||||
|:-----------------------|------------:|--------------:|
|
||||
| begin(values) | 4 | 4 |
|
||||
| begin(values, colours) | 12 | 4 |
|
||||
| map2RGB(value) | 64 - 132 | 2 - 3 |
|
||||
| map2_565(value) | 68 - 140 | 2 - 3 |
|
||||
| function call | time UNO | time ESP32 |
|
||||
|:-------------------------|------------:|-------------:|
|
||||
| begin(values) | 4 | 4 |
|
||||
| begin(values, colours) | 12 | 4 |
|
||||
| map2RGB(value) | 64 - 132 | 2 - 3 |
|
||||
| map2_565(value) | 68 - 140 | 2 - 3 |
|
||||
|
||||
|
||||
#### version 0.1.4
|
||||
|
||||
| function call | time us UNO | time us ESP32 | notes |
|
||||
|:-----------------------|------------:|--------------:|:----------------------|
|
||||
| begin(values) | 284 | 15 | unexpected peak ESP32 |
|
||||
| begin(values, colours) | 304 | 6 |
|
||||
| map2RGB(value) | 40 - 104 | 1 - 2 |
|
||||
| map2_565(value) | 44 - 112 | 1 - 2 |
|
||||
|
||||
|
||||
Note: UNO at 16 MHz, ESP32 at 240 MHz
|
||||
| function call | time UNO | time ESP32 | notes |
|
||||
|:-------------------------|------------:|-------------:|:------------------------|
|
||||
| begin(values) | 284 | 15 | unexpected peak ESP32 |
|
||||
| begin(values, colours) | 304 | 6 |
|
||||
| map2RGB(value) | 40 - 104 | 1 - 2 |
|
||||
| map2_565(value) | 44 - 112 | 1 - 2 |
|
||||
|
||||
|
||||
#### optimization 0.1.4
|
||||
@ -174,30 +175,41 @@ Note: the gain for the ESP32 is less pronounced, but can still be interesting.
|
||||
|
||||
## Future
|
||||
|
||||
#### must
|
||||
#### Must
|
||||
|
||||
- update documentation
|
||||
|
||||
#### should
|
||||
#### Should
|
||||
|
||||
- redo **begin()** of map2colour to allow all values. (0.2.0)
|
||||
- non-decreasing array (already experimental in 0.1.5, will still return false! )
|
||||
- any array of numbers. (return value will always be true then)
|
||||
- any array of numbers. (return value will always be true then)
|
||||
- look for optimizations.
|
||||
- cache last value?
|
||||
|
||||
#### could
|
||||
#### Could
|
||||
|
||||
- make size configurable ?
|
||||
- at least smaller than 7 (speeds up)
|
||||
- ...
|
||||
- **void adjustColour(uint8_t index, uint32_t RGB)**
|
||||
- single colour adjust
|
||||
- faster than calling begin() again
|
||||
- divfactors need to be calculated again.
|
||||
- map2RGB variant that gives a colour to the delta with previous value
|
||||
- user can do that fairly easy => example
|
||||
- map2HSL() as extra colour space.
|
||||
- add **reset()** for default array? (RAM)
|
||||
|
||||
#### wont
|
||||
#### Wont
|
||||
|
||||
- **uint32_t dumpColourMap()** ==> not needed
|
||||
- PROGMEM for default array? ==> slower, AVR specific.
|
||||
- move up the test for non-increase in **begin()** ==> fail fast.
|
||||
- conflicts with begin of fast version.
|
||||
|
||||
- should a 4th (alpha) channel be enabled?
|
||||
- not needed yet, would need new constants
|
||||
- faster 16 bit 565 version?
|
||||
- only on request as a separate class map2colour565.
|
||||
- map2HSL() as extra colour space.
|
||||
- not seen much HSL displays in "arduino" world
|
||||
|
||||
|
@ -1,11 +1,10 @@
|
||||
//
|
||||
// FILE: map2colour_performance.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: map2colour demo
|
||||
// DATE: 2021-12-04
|
||||
// URL: https://github.com/RobTillaart/map2colour
|
||||
|
||||
// NOTE: perfomance depends on colours used (amount of interpolation!)
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "map2colour.h"
|
||||
@ -71,6 +70,8 @@ void setup()
|
||||
delay(10);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.print("TOTAL:\t");
|
||||
Serial.println(total);
|
||||
Serial.print("AVG:\t");
|
||||
Serial.println(total / 103.0);
|
||||
Serial.println();
|
||||
@ -94,6 +95,8 @@ void setup()
|
||||
delay(10);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.print("TOTAL:\t");
|
||||
Serial.println(total);
|
||||
Serial.print("AVG:\t");
|
||||
Serial.println(total / 103.0);
|
||||
Serial.println();
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/map2colour.git"
|
||||
},
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.6",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=map2colour
|
||||
version=0.1.5
|
||||
version=0.1.6
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for mapping a float to colour spectrum
|
||||
|
@ -1,11 +1,9 @@
|
||||
//
|
||||
// FILE: map2colour.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.5
|
||||
// VERSION: 0.1.6
|
||||
// PURPOSE: Arduino library for mapping a float to colour spectrum
|
||||
// URL: https://github.com/RobTillaart/map2colour
|
||||
//
|
||||
// HISTORY see changelog.md
|
||||
|
||||
|
||||
#include "map2colour.h"
|
||||
@ -18,7 +16,7 @@ map2colour::map2colour()
|
||||
|
||||
bool map2colour::begin(float * values, uint32_t * colourMap)
|
||||
{
|
||||
// split colour map in channels
|
||||
// split colour map in channels to allow interpolation per channel
|
||||
if (colourMap != NULL)
|
||||
{
|
||||
for (int i = 0; i < 7; i++)
|
||||
@ -44,7 +42,7 @@ bool map2colour::begin(float * values, uint32_t * colourMap)
|
||||
|
||||
uint32_t map2colour::map2RGB(float value)
|
||||
{
|
||||
int index = 1;
|
||||
uint8_t index = 1;
|
||||
// default values + out of lower range
|
||||
uint8_t R = _Red[0];
|
||||
uint8_t G = _Green[0];
|
||||
@ -52,7 +50,7 @@ uint32_t map2colour::map2RGB(float value)
|
||||
|
||||
if (_values[0] < value)
|
||||
{
|
||||
if (value <= _values[6] )
|
||||
if (value < _values[6] )
|
||||
{
|
||||
// search the interval
|
||||
while (_values[index] < value) index++;
|
||||
@ -61,6 +59,7 @@ uint32_t map2colour::map2RGB(float value)
|
||||
R = _Red[index];
|
||||
G = _Green[index];
|
||||
B = _Blue[index];
|
||||
|
||||
// calculate the interpolation factor
|
||||
// map2colourFast uses pre calculated dividers (costs 24 bytes extra RAM).
|
||||
float factor = (_values[index] - value) / (_values[index] - _values[index - 1]);
|
||||
@ -97,20 +96,19 @@ uint16_t map2colour::map2_565(float value)
|
||||
uint32_t RGB = map2RGB(value);
|
||||
uint16_t colour = 0x0000;
|
||||
RGB >>= 3;
|
||||
colour |= (RGB & 0x001F); // B channel 5 bits
|
||||
colour |= (RGB & 0x001F); // B channel 5 bits
|
||||
RGB >>= 2;
|
||||
colour |= (RGB & 0x07E0); // G channel 6 bits
|
||||
colour |= (RGB & 0x07E0); // G channel 6 bits
|
||||
RGB >>= 3;
|
||||
colour |= (RGB & 0xF800); // R channel 5 bits
|
||||
colour |= (RGB & 0xF800); // R channel 5 bits
|
||||
return colour;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED CLASS
|
||||
// DERIVED CLASS
|
||||
//
|
||||
|
||||
map2colourFast::map2colourFast() : map2colour()
|
||||
{
|
||||
}
|
||||
@ -147,7 +145,7 @@ uint32_t map2colourFast::map2RGB(float value)
|
||||
|
||||
if (_values[0] < value)
|
||||
{
|
||||
if (value <= _values[6] )
|
||||
if (value < _values[6] )
|
||||
{
|
||||
// search the interval
|
||||
while (_values[index] < value) index++;
|
||||
@ -171,7 +169,7 @@ uint32_t map2colourFast::map2RGB(float value)
|
||||
}
|
||||
else
|
||||
{
|
||||
// out of upper range
|
||||
// out of upper range
|
||||
R = _Red[6];
|
||||
G = _Green[6];
|
||||
B = _Blue[6];
|
||||
@ -186,19 +184,21 @@ uint32_t map2colourFast::map2RGB(float value)
|
||||
}
|
||||
|
||||
|
||||
// could be slightly faster (~0.4 us) for AVR by
|
||||
// - split of R = RGB >> 16 (one third could be 16 bit math.
|
||||
uint16_t map2colourFast::map2_565(float value)
|
||||
{
|
||||
uint32_t RGB = map2RGB(value);
|
||||
uint16_t colour = 0x0000;
|
||||
RGB >>= 3;
|
||||
colour |= (RGB & 0x001F); // B channel 5 bits
|
||||
colour |= (RGB & 0x001F); // B channel 5 bits
|
||||
RGB >>= 2;
|
||||
colour |= (RGB & 0x07E0); // G channel 6 bits
|
||||
colour |= (RGB & 0x07E0); // G channel 6 bits
|
||||
RGB >>= 3;
|
||||
colour |= (RGB & 0xF800); // R channel 5 bits
|
||||
colour |= (RGB & 0xF800); // R channel 5 bits
|
||||
return colour;
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -2,15 +2,14 @@
|
||||
//
|
||||
// FILE: map2colour.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.5
|
||||
// VERSION: 0.1.6
|
||||
// PURPOSE: Arduino library for mapping a float to colour spectrum
|
||||
// URL: https://github.com/RobTillaart/map2colour
|
||||
//
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define MAP2COLOUR_LIB_VERSION (F("0.1.5"))
|
||||
#define MAP2COLOUR_LIB_VERSION (F("0.1.6"))
|
||||
|
||||
|
||||
// https://www.w3.org/wiki/CSS/Properties/color/keywords
|
||||
@ -32,6 +31,23 @@
|
||||
#define M2C_AQUA 0x0000FFFF
|
||||
|
||||
|
||||
// needed?
|
||||
// #define M2C_GRAY_1 0x00101010
|
||||
// #define M2C_GRAY_2 0x00202020
|
||||
// #define M2C_GRAY_3 0x00303030
|
||||
// #define M2C_GRAY_4 0x00404040
|
||||
// #define M2C_GRAY_5 0x00505050
|
||||
// #define M2C_GRAY_6 0x00606060
|
||||
// #define M2C_GRAY_7 0x00707070
|
||||
// #define M2C_GRAY_8 0x00808080
|
||||
// #define M2C_GRAY_9 0x00909090
|
||||
// #define M2C_GRAY_10 0x00A0A0A0
|
||||
// #define M2C_GRAY_11 0x00B0B0B0
|
||||
// #define M2C_GRAY_12 0x00C0C0C0
|
||||
// #define M2C_GRAY_13 0x00D0D0D0
|
||||
// #define M2C_GRAY_14 0x00E0E0E0
|
||||
|
||||
|
||||
class map2colour
|
||||
{
|
||||
public:
|
||||
@ -50,6 +66,7 @@ class map2colour
|
||||
uint8_t _Red[7] = { 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF };
|
||||
uint8_t _Green[7] = { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF };
|
||||
uint8_t _Blue[7] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
|
||||
// uint8_t _Alpha[7] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
};
|
||||
|
||||
|
||||
@ -72,5 +89,6 @@ class map2colourFast : public map2colour
|
||||
float divFactor[6];
|
||||
};
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -31,6 +31,7 @@ unittest_setup()
|
||||
fprintf(stderr, "MAP2COLOUR_LIB_VERSION: %s\n", (char *) MAP2COLOUR_LIB_VERSION);
|
||||
}
|
||||
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
@ -95,7 +96,6 @@ unittest(test_constants)
|
||||
}
|
||||
|
||||
|
||||
|
||||
unittest(test_compare_RGB)
|
||||
{
|
||||
map2colour mc;
|
||||
@ -148,8 +148,8 @@ unittest(test_compare_RGB565)
|
||||
}
|
||||
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user