From 5e69b344f8f74aff026783b1997e7226738d2598 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 22 Mar 2024 23:52:46 -0400 Subject: [PATCH] created draw function to reduce string section size --- basic.c | 119 ++++++++++++++++++++++++++++++------------------------ basic.h | 34 ++++++++-------- color.h | 5 --- draw.c | 10 +++++ draw.h | 10 +++++ full.c | 120 ++++++++++++++++++++++++++++++------------------------- full.h | 2 +- justfile | 2 +- main.c | 2 +- 9 files changed, 171 insertions(+), 133 deletions(-) delete mode 100644 color.h create mode 100644 draw.c create mode 100644 draw.h diff --git a/basic.c b/basic.c index 3416f69..f25db0c 100644 --- a/basic.c +++ b/basic.c @@ -2,79 +2,94 @@ #include +#include "draw.h" + void rainbow_8() { - printf( - RED STRIPE - L_RED STRIPE - L_YELLOW STRIPE - GREEN STRIPE - BLUE STRIPE - MAGENTA STRIPE - ); + color colors [] = { + RED, + L_RED, + L_YELLOW, + GREEN, + BLUE, + MAGENTA + }; + draw(6, colors); } void aroace_8() { - printf( - YELLOW STRIPE - L_YELLOW STRIPE - WHITE STRIPE - L_CYAN STRIPE - BLUE STRIPE - ); + color colors [] = { + YELLOW, + L_YELLOW, + WHITE, + L_CYAN, + BLUE + }; + draw(5, colors); } void bisexual_8() { - printf( - RED STRIPE STRIPE - MAGENTA STRIPE - BLUE STRIPE STRIPE - ); + color colors [] = { + RED, + RED, + MAGENTA, + BLUE, + BLUE + }; + draw(5, colors); } void gay_8() { - printf( - CYAN STRIPE - L_CYAN STRIPE - WHITE STRIPE - L_BLUE STRIPE - BLUE STRIPE - ); + color colors [] = { + CYAN, + L_CYAN, + WHITE, + L_BLUE, + BLUE + }; + draw(5, colors); } void lesbian_8() { - printf( - RED STRIPE - L_RED STRIPE - WHITE STRIPE - L_MAGENTA STRIPE - MAGENTA STRIPE - ); + color colors [] = { + RED, + L_RED, + WHITE, + L_MAGENTA, + MAGENTA + }; + draw(5, colors); } void nonbinary_8() { - printf( - L_YELLOW STRIPE - WHITE STRIPE - MAGENTA STRIPE - BLACK STRIPE - ); + color colors [] = { + L_YELLOW, + WHITE, + MAGENTA, + BLACK + }; + draw(4, colors); } void pansexual_8() { - printf( - MAGENTA STRIPE STRIPE - L_YELLOW STRIPE STRIPE - CYAN STRIPE STRIPE - ); + color colors [] = { + MAGENTA, + MAGENTA, + L_YELLOW, + L_YELLOW, + CYAN, + CYAN + }; + draw(6, colors); } void transgender_8() { - printf( - L_CYAN STRIPE - L_MAGENTA STRIPE - WHITE STRIPE - L_MAGENTA STRIPE - L_CYAN STRIPE - ); + color colors [] = { + L_CYAN, + L_MAGENTA, + WHITE, + L_MAGENTA, + L_CYAN + }; + draw(5, colors); } diff --git a/basic.h b/basic.h index f9579f6..13cc7fb 100644 --- a/basic.h +++ b/basic.h @@ -1,23 +1,21 @@ #pragma once -#include "color.h" - -#define BLACK COLOR(0) -#define RED COLOR(1) -#define GREEN COLOR(2) -#define YELLOW COLOR(3) -#define BLUE COLOR(4) -#define MAGENTA COLOR(5) -#define CYAN COLOR(6) -#define GRAY COLOR(7) -#define L_BLACK COLOR(8) -#define L_RED COLOR(9) -#define L_GREEN COLOR(10) -#define L_YELLOW COLOR(11) -#define L_BLUE COLOR(12) -#define L_MAGENTA COLOR(13) -#define L_CYAN COLOR(14) -#define WHITE COLOR(15) +#define BLACK 0 +#define RED 1 +#define GREEN 2 +#define YELLOW 3 +#define BLUE 4 +#define MAGENTA 5 +#define CYAN 6 +#define GRAY 7 +#define L_BLACK 8 +#define L_RED 9 +#define L_GREEN 10 +#define L_YELLOW 11 +#define L_BLUE 12 +#define L_MAGENTA 13 +#define L_CYAN 14 +#define WHITE 15 void rainbow_8(); void aroace_8(); diff --git a/color.h b/color.h deleted file mode 100644 index 92ef0a3..0000000 --- a/color.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#define COLOR(n) "\x1b[38;5;" #n "m" -#define STRIPE "██████████████████\n" - diff --git a/draw.c b/draw.c new file mode 100644 index 0000000..da7f1e2 --- /dev/null +++ b/draw.c @@ -0,0 +1,10 @@ +#include "draw.h" + +#include + +void draw(unsigned short int len, color colors []) { + for(int i = 0; i < len; i++) { + printf(ANSI "%dm" STRIPE, colors[i]); + } +} + diff --git a/draw.h b/draw.h new file mode 100644 index 0000000..00cc7d4 --- /dev/null +++ b/draw.h @@ -0,0 +1,10 @@ +#pragma once + +#define ANSI "\x1b[38;5;" +#define COLOR(n) ANSI #n "m" +#define STRIPE "██████████████████\n" + +typedef unsigned short int color; + +void draw(unsigned short int len, color colors[]); + diff --git a/full.c b/full.c index 6c78ead..67f0921 100644 --- a/full.c +++ b/full.c @@ -2,83 +2,93 @@ #include -#include "color.h" +#include "draw.h" void rainbow_256() { - printf( - COLOR(196) STRIPE // red - COLOR(208) STRIPE // orange - COLOR(220) STRIPE // yellow - COLOR(28) STRIPE // green - COLOR(21) STRIPE // blue - COLOR(90) STRIPE // purple - ); + color colors [] = { + 196, // red + 208, // orange + 220, // yellow + 28, // green + 21, // blue + 90 // purple + }; + draw(6, colors); } void aroace_256() { - printf( - COLOR(172) STRIPE - COLOR(184) STRIPE - COLOR(255) STRIPE - COLOR(38) STRIPE - COLOR(17) STRIPE - ); + color colors [] = { + 172, // orange + 184, // yellow + 255, // white + 38, // blue + 17 // navy + }; + draw(5, colors); } void bisexual_256() { - printf( - COLOR(161) STRIPE STRIPE - COLOR(91) STRIPE - COLOR(21) STRIPE STRIPE - ); + color colors [] = { + 161, // magenta + 161, + 91, // purple + 21, + 21 // blue + }; + draw(5, colors); } void gay_256() { - printf( - COLOR(29) STRIPE - COLOR(49) STRIPE - COLOR(123) STRIPE - COLOR(255) STRIPE - COLOR(75) STRIPE - COLOR(63) STRIPE - COLOR(56) STRIPE - ); + color colors [] = { + 29, + 49, // greens + 123, + 255, // white + 75, + 63, // blues + 56 + }; + draw(7, colors); } void lesbian_256() { - printf( - COLOR(202) STRIPE - COLOR(209) STRIPE - COLOR(255) STRIPE - COLOR(134) STRIPE - COLOR(161) STRIPE - ); + color colors [] = { + 202, // orange + 209, // tangerine + 255, // white + 134, // pink + 161 // magenta + }; + draw(5, colors); } void nonbinary_256() { - printf( - COLOR(226) STRIPE - COLOR(255) STRIPE - COLOR(134) STRIPE - COLOR(16) STRIPE - ); + color colors [] = { + 226, // yellow + 255, // white + 134, // purple + 16 // black + }; + draw(4, colors); } void pansexual_256() { - printf( - COLOR(161) STRIPE STRIPE - COLOR(220) STRIPE STRIPE - COLOR(45) STRIPE STRIPE - ); + color colors [] = { + 161, 161, // magenta + 220, 220, // yellow + 45, 45 // cyan + }; + draw(6, colors); } void transgender_256() { - printf( - COLOR(45) STRIPE - COLOR(117) STRIPE - COLOR(255) STRIPE - COLOR(117) STRIPE - COLOR(45) STRIPE - ); + color colors [] = { + 45, // blue + 117, // pink + 255, // white + 117, // pink + 45 // blue + }; + draw(5, colors); } diff --git a/full.h b/full.h index 9a42ffb..c4e121d 100644 --- a/full.h +++ b/full.h @@ -1,6 +1,6 @@ #pragma once -#include "color.h" +#include "draw.h" void rainbow_256(); void aroace_256(); diff --git a/justfile b/justfile index 24dfbb1..ee66722 100644 --- a/justfile +++ b/justfile @@ -1,4 +1,4 @@ build: - gcc main.c basic.c full.c -o pride-c + gcc main.c draw.c basic.c full.c -o pride-c diff --git a/main.c b/main.c index a6ece88..a53f23a 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,7 @@ #include #include -#include "color.h" +#include "draw.h" #include "basic.h" #include "full.h"