Skip to content

Instantly share code, notes, and snippets.

@borgel
Created June 10, 2018 23:51
Show Gist options
  • Save borgel/d9a8bc11aeb5e0005d8320026c46f6f7 to your computer and use it in GitHub Desktop.
Save borgel/d9a8bc11aeb5e0005d8320026c46f6f7 to your computer and use it in GitHub Desktop.
#include "color.h"
#include "utilities.h"
#include <math.h>
/*
* Algorithm adapted from https://gist.github.com/hdznrrd/656996. Uses a little libmath.
* */
void color_HSV2RGB(struct color_ColorHSV const *hsv, struct color_ColorRGB *rgb) {
int i;
float f,p,q,t;
float h, s, v;
//expand the u8 hue in range 0->255 to 0->359* (there are problems at exactly 360)
h = 359.0 * ((float)hsv->h / 255.0);
h = MAX(0.0, MIN(360.0, h));
s = MAX(0.0, MIN(100.0, hsv->s));
v = MAX(0.0, MIN(100.0, hsv->v));
s /= 100;
v /= 100;
if(s == 0) {
// Achromatic (grey)
rgb->r = rgb->g = rgb->b = round(v*255);
return;
}
h /= 60; // sector 0 to 5
i = floor(h);
f = h - i; // factorial part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch(i) {
case 0:
rgb->r = round(255*v);
rgb->g = round(255*t);
rgb->b = round(255*p);
break;
case 1:
rgb->r = round(255*q);
rgb->g = round(255*v);
rgb->b = round(255*p);
break;
case 2:
rgb->r = round(255*p);
rgb->g = round(255*v);
rgb->b = round(255*t);
break;
case 3:
rgb->r = round(255*p);
rgb->g = round(255*q);
rgb->b = round(255*v);
break;
case 4:
rgb->r = round(255*t);
rgb->g = round(255*p);
rgb->b = round(255*v);
break;
default: // case 5:
rgb->r = round(255*v);
rgb->g = round(255*p);
rgb->b = round(255*q);
}
}
#ifndef COLOR_H__
#define COLOR_H__
/*
* Generic colors and utilities. Derived from
* https://github.com/borgel/sympetrum-v2
*/
#include <stdint.h>
struct color_ColorRGB {
uint8_t b;
uint8_t g;
uint8_t r;
};
#define HSV_CHANNEL_MAX 255
#define HSV_CHANNEL_MIN 0
struct color_ColorHSV {
uint8_t h;
uint8_t s;
uint8_t v;
};
void color_HSV2RGB(struct color_ColorHSV const *hsv, struct color_ColorRGB *rgb);
#endif//COLOR_H__
#ifndef UTILITIES_H__
#define UTILITIES_H__
#define MAX(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define MIN(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _b : _a; })
#endif//UTILITIES_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment