Split file 'colors.c'

This commit is contained in:
2017-03-18 01:22:24 -07:00
parent 2180a4126e
commit 8332c16704
2 changed files with 39 additions and 92 deletions

View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include "colors.h"
#define ASCII_ZERO 48 #define ASCII_ZERO 48
#define ASCII_NINE 57 #define ASCII_NINE 57
#define MIN_CONTRAST 0.5 #define MIN_CONTRAST 0.5
@ -18,19 +19,11 @@
// instead of concatenating strings, they will be printed in parts. // instead of concatenating strings, they will be printed in parts.
// can either be RGB or HLS // can either be RGB or HLS
struct Rgb {
int r, g, b;
};
struct Hls { void hsl2rgb(struct Rgb *dest, const struct Hls const *src) {
int h; if ((src -> h < 0 || 360 < src -> h) ||
float l, s; (src -> s < 0 && 1 < src -> s) ||
}; (src -> l < 0 && 1 < src -> l)) {
void cvt_rgb(struct Rgb *dest, const struct Hls const *src) {
if ((0 > src -> h || src -> h > 360) ||
(0 > src -> s && src -> s > 1) ||
(0 > src -> l && src -> l > 1)) {
return; return;
} }
float r, g, b; float r, g, b;
@ -72,7 +65,7 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) {
dest -> b = ceil((b + m) * 255); dest -> b = ceil((b + m) * 255);
} }
void cvt_hls(struct Hls *dest, const struct Rgb const *src) { void rgb2hsl(struct Hls *dest, const struct Rgb const *src) {
if (src == NULL || dest == NULL) { if (src == NULL || dest == NULL) {
return; return;
} }
@ -101,7 +94,7 @@ void cvt_hls(struct Hls *dest, const struct Rgb const *src) {
} }
} }
void dec(const int d) { static void decspan(const int d) {
switch(d) { switch(d) {
case 0: case 0:
printf("<span style='color:rgb(128,128,128)'>"); printf("<span style='color:rgb(128,128,128)'>");
@ -136,7 +129,7 @@ void dec(const int d) {
} }
} }
void hex(const char *str) { static void hexspan(const char *str) {
const char h1[2] = {str[0], '\0'}; const char h1[2] = {str[0], '\0'};
const char h2[2] = {str[1], '\0'}; const char h2[2] = {str[1], '\0'};
const char h3[2] = {str[2], '\0'}; const char h3[2] = {str[2], '\0'};
@ -145,25 +138,25 @@ void hex(const char *str) {
nrgb.g = strtol(h2, NULL, 16); nrgb.g = strtol(h2, NULL, 16);
nrgb.b = strtol(h3, NULL, 16); nrgb.b = strtol(h3, NULL, 16);
struct Hls nhls; struct Hls nhls;
cvt_hls(&nhls, &nrgb); rgb2hsl(&nhls, &nrgb);
if (nhls.l < MIN_CONTRAST) { if (nhls.l < MIN_CONTRAST) {
nhls.l = MIN_CONTRAST; nhls.l = MIN_CONTRAST;
cvt_rgb(&nrgb, &nhls); hsl2rgb(&nrgb, &nhls);
} }
printf("<span style=\"color:rgb(%d,%d,%d)\">", nrgb.r, nrgb.g, nrgb.b); printf("<span style=\"color:rgb(%d,%d,%d)\">", nrgb.r, nrgb.g, nrgb.b);
} }
void b(char * const str) { static void b(char * const str) {
#define IS_DIGIT(x) (x >= ASCII_ZERO && x <= ASCII_NINE) #define IS_DIGIT(x) (x >= ASCII_ZERO && x <= ASCII_NINE)
char *token = strtok(str, "^"); char *token = strtok(str, "^");
char c; char c;
while (token) { while (token) {
c = token[0]; c = token[0];
if (IS_DIGIT(c)) { if (IS_DIGIT(c)) {
dec(c - ASCII_ZERO); decspan(c - ASCII_ZERO);
printf("%s", token + 1); printf("%s", token + 1);
} else if (c == 'x' && strlen(token) > 4) { } else if (c == 'x' && strlen(token) > 4) {
hex(token + 1); //exclude x hexspan(token + 1); //exclude x
printf("%s", token + 4); printf("%s", token + 4);
} else { } else {
printf("%s", token); printf("%s", token);
@ -174,7 +167,7 @@ void b(char * const str) {
} }
// O(n) // O(n)
void a(const char* str) { void print_plname(const char* str) {
//find instance of ^^ //find instance of ^^
//replace with ^ //replace with ^
//concatenate with rest of the string //concatenate with rest of the string
@ -189,74 +182,3 @@ void a(const char* str) {
free(copy); free(copy);
} }
void testconversion(int r, int g, int b) {
struct Rgb x;
x.r = r;
x.g = g;
x.b = b;
struct Hls y;
cvt_hls(&y, &x);
printf("%d %f %f\n", y.h, y.s, y.l);
}
void testconvhls(int h, float s, float l) {
struct Hls x;
x.h = h;
x.s = s;
x.l = l;
struct Rgb y;
cvt_rgb(&y, &x);
printf("%d %d %d\n", y.r, y.g, y.b);
}
int main() {
/* a("^xf00Unregistered Player^7");
a("^9[^1S^9]^x469Kom^0ier^7");
a("^9sjn^5|^9F^5e^9tus^7");
a("^0[^7>>^0]^xaefdizzy^7"); */
/* testconversion(10, 15, 12);
testconversion(5, 5, 5);
testconversion(3, 15, 10);
testconversion(7, 8, 9);
testconvhls(144, 0.20, 0.5);
testconvhls(0, 0, 0.2);
testconvhls(154, 0.67, 0.035);
testconvhls(210, 0.125, 0.03);
testconvhls(60, 1, 0.25);
testconvhls(180, 1, 0.25);
testconvhls(240, 1, 0.25); */
testconversion(0, 0, 0);
testconversion(255, 255, 255);
testconversion(255, 0, 0);
testconversion(0, 255, 0);
testconversion(0, 0, 255);
testconversion(255, 255, 0);
testconversion(0, 255, 255);
testconversion(255, 0, 255);
testconversion(192, 192, 192);
testconversion(128, 128, 128);
testconversion(128, 0, 0);
testconversion(128, 128, 0);
testconversion(0, 128, 0);
testconversion(128, 0, 128);
testconversion(128, 0, 128);
testconversion(0, 128, 128);
testconversion(0, 0, 128);
testconvhls(0, 0, 0);
testconvhls(0, 0, 1);
testconvhls(0, 1, 0.5);
testconvhls(120, 1, 0.5);
testconvhls(240, 1, 0.5);
testconvhls(60, 1, 0.5);
testconvhls(180, 1, 0.5);
testconvhls(300, 1, 0.5);
testconvhls(0, 0, 0.75);
testconvhls(0, 0, 0.5);
testconvhls(0, 1, 0.25);
testconvhls(60, 1, 0.25);
testconvhls(120, 1, 0.25);
testconvhls(300, 1, 0.25);
testconvhls(180, 1, 0.25);
testconvhls(240, 1, 0.25);
}

25
includes/colors.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef COLORS_H
#define COLORS_H
struct Rgb {
int r, g, b;
};
struct Hls {
int h;
float l, s;
};
void hsl2rgb(struct Rgb *, const struct Hls const *);
void rgb2hsl(struct Hls *, const struct Rgb const *);
static void decspan(const int);
static void hexspan(const char *);
static void b(char * const);
void print_plname(const char*);
#endif