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
+87
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
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