Changed flag struct: Now a flag no longer has a 'vertical' attribute, but instead an integer for the bars, additional to the stripes.

This commit is contained in:
Felizitas 2020-10-26 23:39:03 +01:00
parent a5e0a5239a
commit ee6219109c

134
pride.c
View file

@ -20,7 +20,7 @@ typedef struct
typedef struct
{
int stripes;
bool vertical;
int bars;
const char *name;
const char *description;
const color colors[];
@ -29,8 +29,8 @@ typedef struct
/* common 6-striped pride flag */
flag_stripes_t pride_6 = {
.stripes = 6,
.bars = 1,
.name = "pride",
.vertical = false,
.colors = {{.r=0xe7, .g=0x00 ,.b=0x00},
{.r=0xff, .g=0x8c ,.b=0x00},
{.r=0xff, .g=0xef ,.b=0x00},
@ -43,7 +43,7 @@ flag_stripes_t pride_6 = {
/* 7-striped pride flag */
flag_stripes_t pride_7 = {
.stripes = 7,
.vertical = false,
.bars = 1,
.name = "pride-7",
.colors = {{.r=0xff, .g=0x00 ,.b=0x00},
{.r=0xff, .g=0x7f ,.b=0x00},
@ -58,7 +58,7 @@ flag_stripes_t pride_7 = {
/* harvey milk pride flag (the original pride flag) */
flag_stripes_t pride_harvey_milk = {
.stripes = 8,
.vertical = false,
.bars = 1,
.name = "harvey-milk",
.colors = {{.r=0xff, .g=0x69 ,.b=0xb4},
{.r=0xff, .g=0x00 ,.b=0x00},
@ -74,7 +74,7 @@ flag_stripes_t pride_harvey_milk = {
/* philly pride flag */
flag_stripes_t pride_philly = {
.stripes = 8,
.vertical = false,
.bars = 1,
.name = "philly",
.colors = {{.r=0x02, .g=0x02 ,.b=0x02},
{.r=0x78, .g=0x4f ,.b=0x17},
@ -90,7 +90,7 @@ flag_stripes_t pride_philly = {
/* (new) lesbian pride flag */
flag_stripes_t pride_lesbian = {
.stripes = 5,
.vertical = false,
.bars = 1,
.name = "lesbian",
.colors = {{.r=0xd6, .g=0x29 ,.b=0x00},
{.r=0xff, .g=0x9b ,.b=0x55},
@ -103,7 +103,7 @@ flag_stripes_t pride_lesbian = {
/* older (lipstick) lesbian pride flag */
flag_stripes_t pride_lesbian_7 = {
.stripes = 7,
.vertical = false,
.bars = 1,
.name = "lesbian-7",
.colors = {{.r=0xd5, .g=0x2d ,.b=0x00},
{.r=0xef, .g=0x76 ,.b=0x27},
@ -118,7 +118,7 @@ flag_stripes_t pride_lesbian_7 = {
/* bisexual pride flag */
flag_stripes_t pride_bi = {
.stripes = 5,
.vertical = false,
.bars = 1,
.name = "bisexual",
.colors = {{.r=0xd6, .g=0x02 ,.b=0x70},
{.r=0xd6, .g=0x02 ,.b=0x70},
@ -131,7 +131,7 @@ flag_stripes_t pride_bi = {
/* pansexual pride flag */
flag_stripes_t pride_pan = {
.stripes = 3,
.vertical = false,
.bars = 1,
.name = "pansexual",
.colors = {{.r=0xd6, .g=0x02 ,.b=0x70},
{.r=0xff, .g=0xd8 ,.b=0x00},
@ -142,7 +142,7 @@ flag_stripes_t pride_pan = {
/* polysexual pride flag */
flag_stripes_t pride_poly = {
.stripes = 3,
.vertical = false,
.bars = 1,
.name = "polysexual",
.colors = {{.r=0xd6, .g=0x02 ,.b=0x70},
{.r=0x4b, .g=0xb1 ,.b=0x66},
@ -153,7 +153,7 @@ flag_stripes_t pride_poly = {
/* asexual flag */
flag_stripes_t pride_ace = {
.stripes = 4,
.vertical = false,
.bars = 1,
.name = "asexual",
.colors = {{.r=0x00, .g=0x00 ,.b=0x00},
{.r=0xc3, .g=0xc3 ,.b=0xc3},
@ -165,7 +165,7 @@ flag_stripes_t pride_ace = {
/* flag for aromanticism */
flag_stripes_t pride_aro = {
.stripes = 5,
.vertical = false,
.bars = 1,
.name = "aromantic",
.colors = {{.r=0x3d, .g=0xa5 ,.b=0x43},
{.r=0xaa, .g=0xce ,.b=0x79},
@ -178,7 +178,7 @@ flag_stripes_t pride_aro = {
/* trans* pride flag */
flag_stripes_t pride_trans = {
.stripes = 5,
.vertical = false,
.bars = 1,
.name = "trans",
.colors = {{.r=0x55, .g=0xcd ,.b=0xfc},
{.r=0xf7, .g=0xa8 ,.b=0xb8},
@ -191,7 +191,7 @@ flag_stripes_t pride_trans = {
/* flag of the black trans*-community */
flag_stripes_t pride_black_trans = {
.stripes = 5,
.vertical = false,
.bars = 1,
.name = "black-trans",
.colors = {{.r=0x55, .g=0xcd ,.b=0xfc},
{.r=0xf7, .g=0xa8 ,.b=0xb8},
@ -204,19 +204,19 @@ flag_stripes_t pride_black_trans = {
/* nonbinary flag */
flag_stripes_t pride_nonbinary = {
.stripes = 4,
.vertical = false,
.bars = 1,
.name = "nonbinary",
.colors = {{.r=0x9c, .g=0x5c ,.b=0xd4},
{.r=0xff, .g=0xff ,.b=0xff},
{.r=0xfc, .g=0xf4 ,.b=0x34},
{.r=0x00, .g=0x00 ,.b=0x00}
{.r=0xff, .g=0xff ,.b=0xff},
{.r=0xfc, .g=0xf4 ,.b=0x34},
{.r=0x00, .g=0x00 ,.b=0x00}
}
};
/* genderqueer flag */
flag_stripes_t pride_genderqueer = {
.stripes = 3,
.vertical = false,
.bars = 1,
.name = "genderqueer",
.colors = {{.r=0xb5, .g=0x7e ,.b=0xdc},
{.r=0xff, .g=0xff ,.b=0xff},
@ -227,7 +227,7 @@ flag_stripes_t pride_genderqueer = {
/* neutrois flag */
flag_stripes_t pride_neutrois = {
.stripes = 3,
.vertical = false,
.bars = 1,
.name = "neutrois",
.colors = {{.r=0xff, .g=0xff ,.b=0xff},
{.r=0x36, .g=0xab ,.b=0x4f},
@ -237,8 +237,8 @@ flag_stripes_t pride_neutrois = {
/* androgyne flag */
flag_stripes_t pride_androgyne = {
.stripes = 3,
.vertical = true,
.stripes = 1,
.bars = 3,
.name = "androgyne",
.colors = {{.r=0xfd, .g=0x00 ,.b=0x7e},
{.r=0x98, .g=0x33 ,.b=0xff},
@ -249,7 +249,7 @@ flag_stripes_t pride_androgyne = {
/* flag for genderfluidity */
flag_stripes_t pride_genderfluid = {
.stripes = 5,
.vertical = false,
.bars = 1,
.name = "genderfluid",
.colors = {{.r=0xee, .g=0x75 ,.b=0xa1},
{.r=0xff, .g=0xff ,.b=0xff},
@ -262,7 +262,7 @@ flag_stripes_t pride_genderfluid = {
/* agender flag */
flag_stripes_t pride_agender = {
.stripes = 7,
.vertical = false,
.bars = 1,
.name = "agender",
.colors = {{.r=0x00, .g=0x00 ,.b=0x00},
{.r=0xba, .g=0xba ,.b=0xba},
@ -277,7 +277,7 @@ flag_stripes_t pride_agender = {
/* demiboy flag */
flag_stripes_t pride_demiboy = {
.stripes = 7,
.vertical = false,
.bars = 1,
.name = "demiboy",
.colors = {{.r=0x7f, .g=0x7f ,.b=0x7f},
{.r=0xc3, .g=0xc5 ,.b=0xc4},
@ -292,7 +292,7 @@ flag_stripes_t pride_demiboy = {
/* demigirl flag */
flag_stripes_t pride_demigirl = {
.stripes = 7,
.vertical = false,
.bars = 1,
.name = "demigirl",
.colors = {{.r=0x7f, .g=0x7f ,.b=0x7f},
{.r=0xc3, .g=0xc5 ,.b=0xc4},
@ -307,7 +307,7 @@ flag_stripes_t pride_demigirl = {
/* bigender flag */
flag_stripes_t pride_bigender = {
.stripes = 7,
.vertical = false,
.bars = 1,
.name = "bigender",
.colors = {{.r=0xc4, .g=0x79 ,.b=0xa0},
{.r=0xec, .g=0xa6 ,.b=0xcb},
@ -347,11 +347,16 @@ flag_stripes_t *flags[] = {
&pride_bigender,
NULL};
/* prints bars */
void print_bar(int width, int height, color upper, color lower, bool first)
/* prints bars of a defined length using the unicode 1/8th block characters */
/* width: Width of the bar in characters. */
/* height: position of the division between FG and BG colors. */
/* upper: BG color, acts as the top color of the bar */
/* lower: FG color, acts as the bottom color. */
/* bg_top: Don't show the upper color, instead use an empty (reset) background. */
void print_bar(int width, int height, color upper, color lower, bool bg_top)
{
if(first){
if(bg_top){
COLOR_RESET;
}else{
COLOR_BG(upper);
@ -391,66 +396,41 @@ void print_bar(int width, int height, color upper, color lower, bool first)
return;
}
/* prints scaled flags made solely made from vertical bars */
int flag_sized_horizontal(flag_stripes_t *flag, int size)
/* prints scaled flags */
/* if scaling isn't possible, the function returns '1', otherwise it returns '0'. */
/* Scaling fails if the size is smaller than the amount of (horizontal) stripes */
/* *flag: The flag to be printed. */
/* size: The desired size (in lines). */
int flag_sized(flag_stripes_t *flag, int size)
{
if(flag->stripes <= size){
int stripewidth = (size * 8) / flag->stripes;
int rest = (size * 8) % flag->stripes;
bool first = rest?true:false;
int stripewidth = (size * 8) / flag->stripes;
int rest = (size * 8) % flag->stripes;
bool transparent = rest?true:false;
int breadth = (flag->bars)?((5 * size) / flag->bars):(5 * size);
for(int line = 0; line < size; line++){
int stripe = ((line + 1) * 8 - rest) / stripewidth;
int offset = ((line + 1) * 8 - rest) % stripewidth;
print_bar(5 * size, offset, flag->colors[(first)?(stripe):(stripe-1)], flag->colors[stripe], first);
int stripe = (line * 8 - rest) / stripewidth;
int offset = (line * 8 - rest) % stripewidth;
for(int bar = 1; bar <= flag->bars; bar++){
color fg = flag->colors[(stripe + 1) * bar - 1];
color bg = flag->colors[(stripe + 1) * bar - 1];
print_bar(breadth, offset, bg, fg, transparent);
}
putchar('\n');
first = false;
transparent = false;
}
return 0;
}
return 1;
}
/* prints flags made solely made from horizontal bars */
void flag_horizontal(flag_stripes_t *flag)
{
for(int i=0; i<flag->stripes; i++){
COLOR_BG(flag->colors[i]);
/* aspect ratio is hardcoded here */
for(int spaces=flag->stripes*3; spaces>0; spaces--){
putchar(' ');
}
COLOR_RESET;
putchar('\n');
}
return;
}
/* prints flags made solely made from vertical bars */
void flag_vertical(flag_stripes_t *flag)
{
for(int lines=5; lines>0; lines--){
for(int i=0; i<flag->stripes; i++){
COLOR_BG(flag->colors[i]);
/* aspect ratio is hardcoded here */
for(int spaces = 5; spaces>0; spaces--){
putchar(' ');
}
COLOR_RESET;
}
putchar('\n');
}
return;
}
void flag(flag_stripes_t *flag)
{
if(flag->vertical){
flag_vertical(flag);
}else{
flag_horizontal(flag);
}
flag_sized(flag, 12);
return;
}
@ -488,7 +468,7 @@ int main(int argc, char *argv[])
}
}else{
/* if no option given, print 6-stripe pride-flag */
flag_horizontal(&pride_6);
flag_sized(&pride_6, 6);
}
exit(EXIT_SUCCESS);
}