Skip to content

Instantly share code, notes, and snippets.

@cindrmon
Created October 25, 2021 18:33
Show Gist options
  • Save cindrmon/ee2118ec28631d0853205f7452a43b12 to your computer and use it in GitHub Desktop.
Save cindrmon/ee2118ec28631d0853205f7452a43b12 to your computer and use it in GitHub Desktop.
ANSI Colors Generator from RGB/Hex value in different Languages
import java.math.BigInteger;
/**
* colorises your text using ANSI
*
* @apiNote Most terminals would work with this, some terminals won't
*
* @author cindrmon
*/
public class Colors {
public static final String CLEAR = "\u001B[0m";
/**
*
* selects a color using rgb/hex and converts to ANSI code
*
* @param r - Red value (in decimal)
* @param g - Green value (in decimal)
* @param b - Blue value (in decimal)
* @return escape string with set rgb value
*/
private static String selectColor(int r, int g, int b) {
if (r <= 255 && g <= 255 && b <= 255 && r >= 0 && g >= 0 && b >= 0)
return "\u001B[38;2;" + r + ";" + g + ";" + b + "m";
else
return "\u001B[38;2;255;255;255m";
}
/**
*
* selects a color using rgb/hex and converts to ANSI code
*
* @param hexValue - color in hex value (don't include the '#')
* @return escape string with set rgb value (defaults to white if invalid hex
* value)
*/
private static String selectColor(String hexValue) {
String rHex = "";
String gHex = "";
String bHex = "";
if (hexValue.length() > 6 || !hexValue.matches("[0-9a-f]{6}$"))
return "\u001B[38;2;255;255;255m";
else {
rHex = hexValue.substring(0, 2).toUpperCase();
gHex = hexValue.substring(2, 4).toUpperCase();
bHex = hexValue.substring(4, 6).toUpperCase();
}
// Convert to decimal
BigInteger r = new BigInteger(rHex, 16);
BigInteger g = new BigInteger(gHex, 16);
BigInteger b = new BigInteger(bHex, 16);
return "\u001B[38;2;" + r + ";" + g + ";" + b + "m";
}
/**
*
* colorise text using ANSI characters
*
* @apiNote This does not work with the eclipse console. You must use an
* external console for it to work.
*
* @param text - Text to colorise
* @param r - Red value (in decimal)
* @param g - Green value (in decimal)
* @param b - Blue value (in decimal)
* @return colorised string
*/
public static String colorise(String text, int r, int g, int b) {
String selectedColor = selectColor(r, g, b);
return selectedColor + text + CLEAR;
}
/**
*
* colorise text using ANSI characters
*
* @apiNote This does not work with the eclipse console. You must use an
* external console for it to work.
*
* @param text - Text to colorise
* @param color - hex value (without the '#')
* @return colorised string
*/
public static String colorise(String text, String hexValue) {
String selectedColor = "";
// if it is a hex value
if (hexValue.matches("[0-9a-f]{6}$"))
selectedColor = selectColor(hexValue);
// if it is an invalid hex value
else
selectedColor = CLEAR;
return selectedColor + text + CLEAR;
}
}
class Colors:
@staticmethod
def clear_color():
return "\033[0m"
@staticmethod
def select_color(r, g, b):
return f"\033[38;2;{r};{g};{b}m"
@staticmethod
def select_color_hex(hex_code):
r = int(f"0x{hex_code[0:2]}", 0)
g = int(f"0x{hex_code[2:4]}", 0)
b = int(f"0x{hex_code[4:6]}", 0)
return Colors.select_color(r, g, b)
@staticmethod
def colorise(text, hex_color):
return f"{Colors.select_color_hex(hex_color)}{text}{Colors.clear_color()}"
###
##
# colors.sh
#
# convert to ANSI color using this functions
##
# Function synopsis
#
# clear_color (returns an ANSI escape value to revert a color back to normal)
# select_color <red_value> <green_value> <blue_value> (returns ANSI Escape value for that color)
# select_color_hex <hex_value> (returns ANSI escape value for that color)
# colorise <text_to_colorise> <hex_value> (returns colorised text based on hex value)
##
###
#!/bin/bash
function clear_color() {
echo "\033[0m"
}
function select_color() {
r=$1
g=$2
b=$3
echo "\033[38;2;${r};${g};${b}m"
}
function select_color_hex() {
hex_value=$1
rHex=$(awk '{print substr($0, 1, 2)}' <<< $hex_value)
gHex=$(awk '{print substr($0, 3, 2)}' <<< $hex_value)
bHex=$(awk '{print substr($0, 5, 2)}' <<< $hex_value)
r=$(echo "obase=10; ibase=16; ${rHex^^}" | bc)
g=$(echo "obase=10; ibase=16; ${gHex^^}" | bc)
b=$(echo "obase=10; ibase=16; ${bHex^^}" | bc)
echo $(select_color $r $g $b)
}
function colorise_text() {
text=$1
color=$2
echo -e "$(select_color_hex $color)$text$(clear_color)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment