Browse Source

vertical pattern repeat

this contains awful hacks, the structure of

    print_next_colour(), and
    display()

Really needs to be reworked because they share a lot of state about the
pattern (whether it is forwards or backwards, horizontally and
vertically), but don't explicitly pass any of it between them. I
literally only need to give print_next_colour() two variables (x and y) for it
to get the correct colour, without needing to share super ugly state.

A better option might just be to generate the rainbow in 2x length and
store forwards+reverse. That would save a lot of runtime arithmetic.
dev
alistair 4 years ago
parent
commit
266eb0b83d
  1. 1
      Makefile
  2. 33
      colcat.c

1
Makefile

@ -16,6 +16,5 @@ debug: colcat.c @@ -16,6 +16,5 @@ debug: colcat.c
gcc -g -c c-colours/colours.c -o bin/colours.o
gcc -g -lm bin/colcat.o bin/colours.o -o bin/colcat
clean:
rm bin/*

33
colcat.c

@ -18,6 +18,7 @@ int num_colours; // the length of the rainbow @@ -18,6 +18,7 @@ int num_colours; // the length of the rainbow
// start_hue and end_hue hue values
int start_hue = -1;
int end_hue = -1;
bool reverse = false;
char help_text[] = "\nUsage: colcat [args] [--] [FILES] \n"
"\n"
@ -35,14 +36,10 @@ char help_text[] = "\nUsage: colcat [args] [--] [FILES] \n" @@ -35,14 +36,10 @@ char help_text[] = "\nUsage: colcat [args] [--] [FILES] \n"
/* Print the nth colour of the <rainbow_len> chars long rainbow */
void print_next_colour(int current) {
static bool reverse = false;
if (current % rainbow_len == 0) {
if (current % (num_colours) == 0 && current > 0) {
reverse = !reverse;
}
if (current == 0) {
reverse = false;
}
int index = 0;
if (!reverse) {
@ -72,12 +69,31 @@ bool end_hue_ansi_code(char c) { @@ -72,12 +69,31 @@ bool end_hue_ansi_code(char c) {
int display(FILE *file) {
char c = fgetc(file);
int start_hue = 0;
int start = 0;
int end;
int counter = 0;
if (frequency)
end = num_colours / frequency - 1;
else {
end = 0;
counter = num_colours; // so that print starts at reverse=true
}
bool in_escape = false;
bool vert_reverse = false;
while (c != EOF) {
if (c == '\n') {
counter = (start_hue += frequency);
if (frequency) {
if (counter % (end) == 0) {
vert_reverse = !vert_reverse;
}
counter = start += (vert_reverse ? -1 : 1) * frequency;
if (counter < 0) {
counter = -counter;
}
} else {
counter = num_colours;
}
reverse = false;
}
if (c == '\033') {
in_escape = true;
@ -156,6 +172,9 @@ int generate_rainbow() { @@ -156,6 +172,9 @@ int generate_rainbow() {
colours[i] = get_rgb(c);
}
c.h = end_hue;
colours[num_colours - 1] = get_rgb(c);
return 0;
}

Loading…
Cancel
Save