How ansi work
This commit is contained in:
parent
8fe3efac35
commit
69188b3e66
1 changed files with 13 additions and 0 deletions
13
toAnsi/toAnsi.js
Normal file
13
toAnsi/toAnsi.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
message = '§#123456' // §#RRGGBB
|
||||
|
||||
const hexRegex = /§#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/ // find §#??????
|
||||
while (message.search(hexRegex) !== -1) {
|
||||
const hexCodes = hexRegex.exec(message)
|
||||
const red = parseInt(hexCodes[1], 16) // converts the hexadecimal strings to decimal values.
|
||||
const green = parseInt(hexCodes[2], 16) // The function replaces the hexadecimal color code with an ANSI escape sequence that sets the text color using RGB values.
|
||||
const blue = parseInt(hexCodes[3], 16) // from google
|
||||
message = message.replace(hexRegex, `\u001b[38;2;${red};${green};${blue}m`)
|
||||
message = '\x1B[0m' + message + "Text Message" + '\x1B[0m' // reset + color + "Text" + reset
|
||||
}
|
||||
|
||||
console.log(message)
|
Reference in a new issue