Browse Source

Split file 'colors.c'

master
5 years ago
parent
commit
8332c16704
  1. 106
      includes/colors.c
  2. 25
      includes/colors.h

106
includes/colors.c

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "colors.h"
#define ASCII_ZERO 48
#define ASCII_NINE 57
#define MIN_CONTRAST 0.5
@ -18,19 +19,11 @@ @@ -18,19 +19,11 @@
// instead of concatenating strings, they will be printed in parts.
// can either be RGB or HLS
struct Rgb {
int r, g, b;
};
struct Hls {
int h;
float l, s;
};
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)) {
void hsl2rgb(struct Rgb *dest, const struct Hls const *src) {
if ((src -> h < 0 || 360 < src -> h) ||
(src -> s < 0 && 1 < src -> s) ||
(src -> l < 0 && 1 < src -> l)) {
return;
}
float r, g, b;
@ -72,7 +65,7 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) { @@ -72,7 +65,7 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) {
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) {
return;
}
@ -101,7 +94,7 @@ void cvt_hls(struct Hls *dest, const struct Rgb const *src) { @@ -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) {
case 0:
printf("<span style='color:rgb(128,128,128)'>");
@ -136,7 +129,7 @@ void dec(const int d) { @@ -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 h2[2] = {str[1], '\0'};
const char h3[2] = {str[2], '\0'};
@ -145,25 +138,25 @@ void hex(const char *str) { @@ -145,25 +138,25 @@ void hex(const char *str) {
nrgb.g = strtol(h2, NULL, 16);
nrgb.b = strtol(h3, NULL, 16);
struct Hls nhls;
cvt_hls(&nhls, &nrgb);
rgb2hsl(&nhls, &nrgb);
if (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);
}
void b(char * const str) {
static void b(char * const str) {
#define IS_DIGIT(x) (x >= ASCII_ZERO && x <= ASCII_NINE)
char *token = strtok(str, "^");
char c;
while (token) {
c = token[0];
if (IS_DIGIT(c)) {
dec(c - ASCII_ZERO);
decspan(c - ASCII_ZERO);
printf("%s", token + 1);
} else if (c == 'x' && strlen(token) > 4) {
hex(token + 1); //exclude x
hexspan(token + 1); //exclude x
printf("%s", token + 4);
} else {
printf("%s", token);
@ -174,7 +167,7 @@ void b(char * const str) { @@ -174,7 +167,7 @@ void b(char * const str) {
}
// O(n)
void a(const char* str) {
void print_plname(const char* str) {
//find instance of ^^
//replace with ^
//concatenate with rest of the string
@ -189,74 +182,3 @@ void a(const char* str) { @@ -189,74 +182,3 @@ void a(const char* str) {
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

@ -0,0 +1,25 @@ @@ -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
Loading…
Cancel
Save