diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..00738dc --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +CFLAGS= -Ofast -I"include" +BIN=cts + +all: main + +main: main.o + gcc colors.o dbquery.o main.o -lsqlite3 -o $(BIN) + +main.o: dbquery.o src/main.c + gcc -c src/main.c $(CFLAGS) + +dbquery.o: colors.o src/dbquery.c + gcc -c src/dbquery.c $(CFLAGS) + +colors.o: src/colors.c + gcc -c src/colors.c $(CFLAGS) + +testcolor: src/colors.c src/tcolor.c + gcc src/colors.c src/tcolor.c -o tcolor -I"includes" -g + +clean: + rm *.o *.log $(BIN) diff --git a/includes/colors.h b/include/colors.h similarity index 100% rename from includes/colors.h rename to include/colors.h diff --git a/includes/dbquery.h b/include/dbquery.h similarity index 100% rename from includes/dbquery.h rename to include/dbquery.h diff --git a/makefile b/makefile deleted file mode 100644 index b2d222a..0000000 --- a/makefile +++ /dev/null @@ -1,18 +0,0 @@ -CFLAGS= -Ofast - -all: cts tcolor - -cts: main.o - gcc colors.o sqlite3.o dbquery.o main.o -o cts - -main.o: dbquery.o main.c - gcc -c main.c -I"includes" $(CFLAGS) - -dbquery.o: colors.o includes/dbquery.c - gcc -c includes/dbquery.c $(CFLAGS) - -colors.o: includes/colors.c - gcc -c includes/colors.c $(CFLAGS) - -tcolor: includes/colors.c tcolor.c - gcc includes/colors.c tcolor.c -o tcolor -I"includes" -g \ No newline at end of file diff --git a/includes/colors.c b/src/colors.c similarity index 100% rename from includes/colors.c rename to src/colors.c diff --git a/includes/dbquery.c b/src/dbquery.c similarity index 100% rename from includes/dbquery.c rename to src/dbquery.c diff --git a/main.c b/src/main.c similarity index 100% rename from main.c rename to src/main.c diff --git a/src/tcolor.c b/src/tcolor.c new file mode 100644 index 0000000..69da6ed --- /dev/null +++ b/src/tcolor.c @@ -0,0 +1,182 @@ +#include "colors.h" +#include +#include +#include + +#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); +} +