183 lines
5.0 KiB
C
183 lines
5.0 KiB
C
#include "colors.h"
|
|
#include <stdio.h>
|
|
#include <math.h>
|
|
#include <stdbool.h>
|
|
|
|
#define RGB_BLACK 0,0,0
|
|
#define RGB_WHITE 255,255,255
|
|
#define RGB_RED 255,0,0
|
|
#define RGB_LIME 0,255,0
|
|
#define RGB_BLUE 0,0,255
|
|
#define RGB_YELLOW 255,255,0
|
|
#define RGB_CYAN 0,255,255
|
|
#define RGB_MAGENTA 255,0,255
|
|
#define RGB_SILVER 192,192,192
|
|
#define RGB_GREY 128,128,128
|
|
#define RGB_MAROON 128,0,0
|
|
#define RGB_OLIVE 128,128,0
|
|
#define RGB_GREEN 0,128,0
|
|
#define RGB_PURPLE 128,0,128
|
|
#define RGB_TEAL 0,128,128
|
|
#define RGB_NAVY 0,0,128
|
|
|
|
#define HSL_BLACK 0,0.0f,0.0f
|
|
#define HSL_WHITE 0,0.0f,1.0f
|
|
#define HSL_RED 0,1.0f,0.5f
|
|
#define HSL_LIME 120,1.0f,0.5f
|
|
#define HSL_BLUE 240,1.0f,0.5f
|
|
#define HSL_YELLOW 60,1.0f,0.5f
|
|
#define HSL_CYAN 180,1.0f,0.5f
|
|
#define HSL_MAGENTA 300,1.0f,0.5f
|
|
#define HSL_SILVER 0,0.0f,0.75f
|
|
#define HSL_GREY 0,0.0f,0.50f
|
|
#define HSL_MAROON 0,1.0f,0.25f
|
|
#define HSL_OLIVE 60,1.0f,0.25f
|
|
#define HSL_GREEN 120,1.0f,0.25f
|
|
#define HSL_PURPLE 300,1.0f,0.25f
|
|
#define HSL_TEAL 180,1.0f,0.25f
|
|
#define HSL_NAVY 240,1.0f,0.25f
|
|
|
|
#define ERROR_TOLERANCE 0.01
|
|
|
|
#define CLR4BIT 16
|
|
|
|
bool trgb2hsl(const int r, const int g, const int b,
|
|
const int h, const float s, const float l) {
|
|
bool result = true;
|
|
struct Rgb x;
|
|
x.r = r;
|
|
x.g = g;
|
|
x.b = b;
|
|
struct Hls y;
|
|
rgb2hsl(&y, &x);
|
|
if (y.h == h && fabs(y.s - s) <= ERROR_TOLERANCE && fabs(y.l - l) <= ERROR_TOLERANCE) {
|
|
// printf("COLOR CONVERSION OK!\n");
|
|
;
|
|
} else {
|
|
printf("COLOR CONVERSION UNACCEPTABLE\n\texpected: %d %0.2f %0.2f\n", h, s, l);
|
|
result = false;
|
|
}
|
|
printf("\t%d %0.2f %0.2f\n", y.h, y.s, y.l);
|
|
return result;
|
|
}
|
|
|
|
void test_rgb2hsl(bool * const r) {
|
|
r[0] = trgb2hsl(RGB_BLACK, HSL_BLACK);
|
|
r[1] = trgb2hsl(RGB_WHITE, HSL_WHITE);
|
|
r[2] = trgb2hsl(RGB_RED, HSL_RED);
|
|
r[3] = trgb2hsl(RGB_LIME, HSL_LIME);
|
|
r[4] = trgb2hsl(RGB_BLUE, HSL_BLUE);
|
|
r[5] = trgb2hsl(RGB_YELLOW, HSL_YELLOW);
|
|
r[6] = trgb2hsl(RGB_CYAN, HSL_CYAN);
|
|
r[7] = trgb2hsl(RGB_MAGENTA, HSL_MAGENTA);
|
|
r[8] = trgb2hsl(RGB_SILVER, HSL_SILVER);
|
|
r[9] = trgb2hsl(RGB_GREY, HSL_GREY);
|
|
r[10] = trgb2hsl(RGB_MAROON, HSL_MAROON);
|
|
r[11] = trgb2hsl(RGB_OLIVE, HSL_OLIVE);
|
|
r[12] = trgb2hsl(RGB_GREEN, HSL_GREEN);
|
|
r[13] = trgb2hsl(RGB_PURPLE, HSL_PURPLE);
|
|
r[14] = trgb2hsl(RGB_TEAL, HSL_TEAL);
|
|
r[15] = trgb2hsl(RGB_NAVY, HSL_NAVY);
|
|
}
|
|
|
|
bool thls2rgb(int h, float s, float l, int r, int g, int b) {
|
|
bool result = true;
|
|
struct Hls x;
|
|
x.h = h;
|
|
x.s = s;
|
|
x.l = l;
|
|
struct Rgb y;
|
|
hsl2rgb(&y, &x);
|
|
if (y.r == r && y.g == g && y.b == b) {
|
|
;
|
|
} else {
|
|
printf("COLOR CONVERSION INEXACT\n\texpected: %d %d %d\n", r, g, b);
|
|
result = false;
|
|
}
|
|
printf("%d %d %d\n", y.r, y.g, y.b);
|
|
return result;
|
|
}
|
|
|
|
void test_hsl2rgb(bool * const r) {
|
|
r[0] = thls2rgb(HSL_BLACK, RGB_BLACK);
|
|
r[1] = thls2rgb(HSL_WHITE, RGB_WHITE);
|
|
r[2] = thls2rgb(HSL_RED, RGB_RED);
|
|
r[3] = thls2rgb(HSL_LIME, RGB_LIME);
|
|
r[4] = thls2rgb(HSL_BLUE, RGB_BLUE);
|
|
r[5] = thls2rgb(HSL_YELLOW, RGB_YELLOW);
|
|
r[6] = thls2rgb(HSL_CYAN, RGB_CYAN);
|
|
r[7] = thls2rgb(HSL_MAGENTA, RGB_MAGENTA);
|
|
r[8] = thls2rgb(HSL_SILVER, RGB_SILVER);
|
|
r[9] = thls2rgb(HSL_GREY, RGB_GREY);
|
|
r[10] = thls2rgb(HSL_MAROON, RGB_MAROON);
|
|
r[11] = thls2rgb(HSL_OLIVE, RGB_OLIVE);
|
|
r[12] = thls2rgb(HSL_GREEN, RGB_GREEN);
|
|
r[13] = thls2rgb(HSL_PURPLE, RGB_PURPLE);
|
|
r[14] = thls2rgb(HSL_TEAL, RGB_TEAL);
|
|
r[15] = thls2rgb(HSL_NAVY, RGB_NAVY);
|
|
}
|
|
|
|
void test4bitcolors(void (*f)(bool * const)) {
|
|
const char *colors[CLR4BIT] = {
|
|
"BLACK",
|
|
"WHITE",
|
|
"RED",
|
|
"LIME",
|
|
"BLUE",
|
|
"YELLOW",
|
|
"CYAN",
|
|
"MAGENTA",
|
|
"SILVER",
|
|
"GREY",
|
|
"MAROON",
|
|
"OLIVE",
|
|
"GREEN",
|
|
"PURPLE",
|
|
"TEAL",
|
|
"NAVY"
|
|
};
|
|
bool results[CLR4BIT];
|
|
f(results);
|
|
unsigned int i;
|
|
for (i = 0; i < CLR4BIT; ++i) {
|
|
if (results[i]) {
|
|
printf("%s's OK\n", colors[i]);
|
|
} else {
|
|
printf("%s's INCORRECT\n", colors[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void dpconvert(int r, int g, int b) {
|
|
struct Rgb t;
|
|
t.r = r * 0xf;
|
|
t.g = g * 0xf;
|
|
t.b = b * 0xf;
|
|
struct Hls z;
|
|
rgb2hsl(&z, &t);
|
|
printf("%d %f %f\n", z.h, z.s, z.l);
|
|
if (z.l < 0.5) {
|
|
z.l = 0.5;
|
|
hsl2rgb(&t, &z);
|
|
printf("%d %d %d\n", t.r, t.g, t.b);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
/* print_plname("^xf00Unregistered Player^7");
|
|
print_plname("^9[^1S^9]^x469Kom^0ier^7");
|
|
print_plname("^9sjn^5|^9F^5e^9tus^7");
|
|
print_plname("^0[^7>>^0]^xaefdizzy^7");
|
|
print_plname("^x46^xasdf"); */
|
|
print_plname("^xB90Beemann^7");
|
|
printf("Testing hsl 2 rgb\n");
|
|
test4bitcolors(&test_hsl2rgb);
|
|
printf("Testing rgb 2 hsl\n");
|
|
test4bitcolors(&test_rgb2hsl);
|
|
dpconvert(4, 6, 9);
|
|
trgb2hsl(60, 90, 135, 216, 0.38f, 0.38f);
|
|
trgb2hsl(79, 118, 176, 216, 0.38f, 0.50f);
|
|
thls2rgb(216, 0.38f, 0.50f, 79, 118, 176);
|
|
}
|
|
|