sophuwu.site > crls
Made rainbow library.
sophuwu sophie@sophuwu.site
Sun, 06 Aug 2023 01:53:54 +0200
commit

8d13d2feebe87d837ce7c9fbf3007f9c846257db

parent

977826b8ddff18d09e077c3017091496829dacfe

1 files changed, 42 insertions(+), 18 deletions(-)

jump to
M main.cppmain.cpp

@@ -9,38 +9,39 @@

struct rgb { double r, g, b; - std::string str(char sep = ';') { - return std::to_string((int)r) + sep + std::to_string((int)g) + sep + std::to_string((int)b); - } - - void set(double rr, double gg, double bb) {r = rr;g = gg;b = bb;} - void set(rgb c) {r = c.r;g = c.g;b = c.b;} - void clamp() { if (r > 255) r = 255; else if (r < 0) r = 0; if (g > 255) g = 255; else if (g < 0) g = 0; if (b > 255) b = 255; else if (b < 0) b = 0; } + + std::string str(char sep = ';') { + clamp(); + return std::to_string((int)r) + sep + std::to_string((int)g) + sep + std::to_string((int)b); + } + + void set(double rr, double gg, double bb) {r = rr;g = gg;b = bb; clamp();} + void set(rgb c) {r = c.r;g = c.g;b = c.b; clamp();} void print(std::string s) {printf("\033[38;2;%sm%s\033[0m", str().c_str(), s.c_str());} void printAt(std::string s, int x, int y) {printf("\033[%d;%dH\033[38;2;%sm%s\033[0m", y, x, str().c_str(), s.c_str());} void setPx(int x, int y) {printf("\033[%d;%dH\033[48;2;%sm \033[0m", y, x, str().c_str());} double operator[](int i) { - switch (i) { + switch (i%3) { case 0: return r; case 1: return g; case 2: return b; } - panic("rgb index out of range"); return 0; } void indexAdd(int i, double v) { - switch (i) { + switch (i%3) { case 0: r += v; break; case 1: g += v; break; case 2: b += v; break; } + clamp(); } };

@@ -55,11 +56,11 @@ s = 5.0*255.0 / (double)len;

} void next() { - if (s == 0) panic("Empty infinity loop."); + if (s == 0) panic("Empty infinity loop. Rainbow not initialized."); for (int i = 0; i < 3; i++) { - if ((c[(i+1)%3]==0||c[(i+1)%3]==255)&&(c[(i+2)%3]==0||c[(i+2)%3]==255)) { - if (c[(i+1)%3]==0&&c[(i+2)%3]==255) c.indexAdd(i, s); - else if (c[(i+1)%3]==255&&c[(i+2)%3]==0) c.indexAdd(i, -s); + if ((c[i+1]==0||c[i+1]==255)&&(c[i+2]==0||c[i+2]==255)) { + if (c[i+1]==0&&c[i+2]==255) c.indexAdd(i, s); + else if (c[i+1]==255&&c[i+2]==0) c.indexAdd(i, -s); c.clamp(); } }

@@ -70,18 +71,41 @@ rainbow r;

r.init(len); r.c.set(c); for (int i = 0; i < s.length(); i++) { - r.c.print(s.substr(i, 1)); + r.print(s.substr(i, 1)); r.next(); } } }; +/* example of how to use! int main() { + + // declare a variable of type rainbow rainbow r; - r.init(15); - std::string s = "Hello World!\n"; + + // initialize it with the length you want. it is inversely proportional to the number of colors + // init also sets the starting color to red. change it with r.c.set(r, g, b) (0 to 255) + r.init(30); + + // some string, can be anything. has to be of type std::string, cannot be typed directly into the function + std::string s = "Hello World!"; + + // loops go well with rainbows :) for (int i = 0; i < 30; i++) { + + //prints part of the rainbow + r.print(s); + + printf("\t"); // tab + + // print string with a sub-rainbow of length 30 r.print2d(s); + + printf("\n"); // newline + + // moves to the next colour r.next(); + } -}+} +*/