nothing/src/color.c

98 lines
2.0 KiB
C
Raw Normal View History

2019-05-25 06:29:35 -07:00
#include <SDL.h>
2018-04-29 06:49:25 -07:00
#include <string.h>
2018-01-27 11:12:32 -08:00
2018-04-29 06:58:09 -07:00
#include "color.h"
2018-01-27 11:12:32 -08:00
2018-11-12 04:08:57 -08:00
Color rgba(float r, float g, float b, float a)
2018-01-27 11:12:32 -08:00
{
const Color result = {
2018-01-27 11:12:32 -08:00
.r = r,
.g = g,
.b = b,
.a = a
};
return result;
}
static Uint8 hex2dec_digit(char c)
{
if (c >= '0' && c <= '9') {
return (Uint8) (c - '0');
}
if (c >= 'A' && c <= 'F') {
return (Uint8) (10 + c - 'A');
}
if (c >= 'a' && c <= 'f') {
return (Uint8) (10 + c - 'a');
}
return 0;
}
static Uint8 parse_color_component(const char *component)
{
return (Uint8) (hex2dec_digit(component[0]) * 16 + hex2dec_digit(component[1]));
}
2018-11-12 04:12:17 -08:00
Color hexstr(const char *hexstr)
{
if (strlen(hexstr) != 6) {
2018-11-12 04:08:57 -08:00
return rgba(0.0f, 0.0f, 0.0f, 1.0f);
}
2018-11-12 04:11:13 -08:00
return rgba(
parse_color_component(hexstr) / 255.0f,
parse_color_component(hexstr + 2) / 255.0f,
parse_color_component(hexstr + 4) / 255.0f,
1.0f);
}
SDL_Color color_for_sdl(Color color)
2018-01-27 11:12:32 -08:00
{
const SDL_Color result = {
.r = (Uint8)roundf(color.r * 255.0f),
.g = (Uint8)roundf(color.g * 255.0f),
.b = (Uint8)roundf(color.b * 255.0f),
.a = (Uint8)roundf(color.a * 255.0f)
};
return result;
}
Color color_desaturate(Color c)
{
const float k = (c.r + c.g + c.b) / 3.0f;
2018-11-12 04:08:57 -08:00
return rgba(k, k, k, c.a);
}
Color color_darker(Color c, float d)
{
2018-11-12 04:08:57 -08:00
return rgba(fmaxf(c.r - d, 0.0f),
fmaxf(c.g - d, 0.0f),
fmaxf(c.b - d, 0.0f),
c.a);
}
2019-04-28 11:49:55 -07:00
Color color_invert(Color c)
{
return rgba(1.0f - c.r, 1.0f - c.g, 1.0f - c.b, c.a);
}
Color color_scale(Color c, Color fc)
{
return rgba(
fmaxf(fminf(c.r * fc.r, 1.0f), 0.0f),
fmaxf(fminf(c.g * fc.g, 1.0f), 0.0f),
fmaxf(fminf(c.b * fc.b, 1.0f), 0.0f),
fmaxf(fminf(c.a * fc.a, 1.0f), 0.0f));
}
2019-06-16 13:01:42 -07:00
int color_hex_to_stream(Color color, FILE *stream)
{
SDL_Color sdl = color_for_sdl(color);
return fprintf(stream, "%02x%02x%02x", sdl.r, sdl.g, sdl.b);
}