Ask coding questions

← Back to all posts
Color
RepelDown

How do I add color to my

for my name genarator?
Can anyone with C++ experience please help me? Thanks!

-Repel

Answered by xxpertHacker [earned 5 cycles]
View Answer
Voters
RepelDown
Comments
hotnewtop
Coder100

You can check out the wikipedia page for more information!

xxpertHacker

@Coder100
This is C++, not C:

Coder100

lol ok fine @xxpertHacker

RepelDown

@xxpertHacker Thank you! Where should I put it though?

Coder100

put it usually on the top of your page @RepelDown

xxpertHacker

@RepelDown Depends on what you want to be colored.
Where you put it, everything past it is colored.

ex:

xxpertHacker

@RepelDown If you want, I had an old function for coloring that I used in one of my HTTP servers for color-coding stuff like errors vs normal messages.
(green was good, red was an error)

Just moved the function into a Repl:
https://repl.it/@xxpertHacker/color

It takes wide strings, not ASCII strings.

It is also probably a bit advanced since it uses a template, but hopefully it isn't too complicated.

It takes three template arguments, r for red, g for green, and b for blue.

They all must be constants between 0..255.

xxpertHacker

@RepelDown Did the Repl help?

RepelDown

@xxpertHacker so i can just copy and paste (a bit)?

xxpertHacker

@RepelDown Literally just fork the entire Repl, I don't care.
Also, if it helped, you can hover over that checkmark on the left side <<< of a user and click it to say "this person solved my problem"

RepelDown

@xxpertHacker thats the left side.

xxpertHacker

@RepelDown I say right, but then put arrows pointing left (<), dang.

xxpertHacker

Look into ASNI codes.

Repl uses a Linux VM, so you can color with them.

They often start with the Esc character (escaped, of course).
\e|\x1B. and a left bracket [.

https://en.wikipedia.org/wiki/ANSI_escape_code

Each one corresponds to a separate color, and there is an ending sequence (think it's \e[m).

There is no language-level support for this stuff, it's just platform-specific monitor codes.

Coder100

more like xterm @xxpertHacker

xxpertHacker

@Coder100 Yeah, XTerm terminal commands.

Coder100

also plz dont use namespace std

RepelDown

@Coder100 okayyy

RepelDown

@Coder100 that might take some time

Coder100
Coder100

anyways how is my solution working @RepelDown

xxpertHacker

@Coder100 using namespace::std; isn't even that bad, it's when you use it at global scope that it's bad.

And even then, it can only bite you if you use multiple using namespace:: statements.

RYANTADIPARTHI

Do you mean put color to your code? or put color to your markdown?

RepelDown

@RYANTADIPARTHI color to my code

RYANTADIPARTHI

@RepelDown ok, you can use system for that, try this link:

https://www.geeksforgeeks.org/how-to-print-colored-text-in-c/

xxpertHacker

@RYANTADIPARTHI I like how you suggest worst practice to a beginner. And it only works on Windows. :/