Add colors toolbox (rgb and hsv)

This commit is contained in:
Willy-JL
2023-08-14 01:17:37 +02:00
parent 6f64300aa9
commit 5e8efe8281
3 changed files with 116 additions and 0 deletions

View File

@@ -1733,6 +1733,7 @@ Function,+,hex_char_to_hex_nibble,_Bool,"char, uint8_t*"
Function,+,hex_char_to_uint8,_Bool,"char, char, uint8_t*" Function,+,hex_char_to_uint8,_Bool,"char, char, uint8_t*"
Function,+,hex_chars_to_uint64,_Bool,"const char*, uint64_t*" Function,+,hex_chars_to_uint64,_Bool,"const char*, uint64_t*"
Function,+,hex_chars_to_uint8,_Bool,"const char*, uint8_t*" Function,+,hex_chars_to_uint8,_Bool,"const char*, uint8_t*"
Function,+,hsv2rgb,RgbColor,HsvColor
Function,-,hypot,double,"double, double" Function,-,hypot,double,"double, double"
Function,-,hypotf,float,"float, float" Function,-,hypotf,float,"float, float"
Function,-,hypotl,long double,"long double, long double" Function,-,hypotl,long double,"long double, long double"
@@ -2555,6 +2556,7 @@ Function,-,rfal_platform_spi_acquire,void,
Function,-,rfal_platform_spi_release,void, Function,-,rfal_platform_spi_release,void,
Function,-,rfal_set_callback_context,void,void* Function,-,rfal_set_callback_context,void,void*
Function,-,rfal_set_state_changed_callback,void,RfalStateChangedCallback Function,-,rfal_set_state_changed_callback,void,RfalStateChangedCallback
Function,+,rgb2hsv,HsvColor,RgbColor
Function,+,rgb_backlight_get_color_count,uint8_t, Function,+,rgb_backlight_get_color_count,uint8_t,
Function,+,rgb_backlight_get_color_text,const char*,uint8_t Function,+,rgb_backlight_get_color_text,const char*,uint8_t
Function,+,rgb_backlight_get_settings,RGBBacklightSettings*, Function,+,rgb_backlight_get_settings,RGBBacklightSettings*,
1 entry status name type params
1733 Function + hex_char_to_uint8 _Bool char, char, uint8_t*
1734 Function + hex_chars_to_uint64 _Bool const char*, uint64_t*
1735 Function + hex_chars_to_uint8 _Bool const char*, uint8_t*
1736 Function + hsv2rgb RgbColor HsvColor
1737 Function - hypot double double, double
1738 Function - hypotf float float, float
1739 Function - hypotl long double long double, long double
2556 Function - rfal_platform_spi_release void
2557 Function - rfal_set_callback_context void void*
2558 Function - rfal_set_state_changed_callback void RfalStateChangedCallback
2559 Function + rgb2hsv HsvColor RgbColor
2560 Function + rgb_backlight_get_color_count uint8_t
2561 Function + rgb_backlight_get_color_text const char* uint8_t
2562 Function + rgb_backlight_get_settings RGBBacklightSettings*

87
lib/toolbox/colors.c Normal file
View File

@@ -0,0 +1,87 @@
// https://stackoverflow.com/a/14733008
#include "colors.h"
RgbColor hsv2rgb(HsvColor hsv) {
RgbColor rgb;
uint8_t region, remainder, p, q, t;
if(hsv.s == 0) {
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
}
region = hsv.h / 43;
remainder = (hsv.h - (region * 43)) * 6;
p = (hsv.v * (255 - hsv.s)) >> 8;
q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
switch(region) {
case 0:
rgb.r = hsv.v;
rgb.g = t;
rgb.b = p;
break;
case 1:
rgb.r = q;
rgb.g = hsv.v;
rgb.b = p;
break;
case 2:
rgb.r = p;
rgb.g = hsv.v;
rgb.b = t;
break;
case 3:
rgb.r = p;
rgb.g = q;
rgb.b = hsv.v;
break;
case 4:
rgb.r = t;
rgb.g = p;
rgb.b = hsv.v;
break;
default:
rgb.r = hsv.v;
rgb.g = p;
rgb.b = q;
break;
}
return rgb;
}
HsvColor rgb2hsv(RgbColor rgb) {
HsvColor hsv;
uint8_t rgbMin, rgbMax;
rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
hsv.v = rgbMax;
if(hsv.v == 0) {
hsv.h = 0;
hsv.s = 0;
return hsv;
}
hsv.s = 255 * ((long)rgbMax - (long)rgbMin) / hsv.v;
if(hsv.s == 0) {
hsv.h = 0;
return hsv;
}
if(rgbMax == rgb.r)
hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
else if(rgbMax == rgb.g)
hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
else
hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);
return hsv;
}

27
lib/toolbox/colors.h Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct RgbColor {
uint8_t r;
uint8_t g;
uint8_t b;
} RgbColor;
typedef struct HsvColor {
uint8_t h;
uint8_t s;
uint8_t v;
} HsvColor;
RgbColor hsv2rgb(HsvColor hsv);
HsvColor rgb2hsv(RgbColor rgb);
#ifdef __cplusplus
}
#endif