colors.crumb
is a Crumb usable providing basic terminal styling functions as well as access to RGB, HEX, ANSI conversion functions.
Styling can be applied to strings and also to strings within lists.
- Download https://raw.githubusercontent.com/ronilan/colors.crumb/main/colors.crumb
- Place it in your Crumb project. (see: Crumb Template for an easy starter.)
- Use it.
This code:
(use "./colors.crumb" {
// loop forever
(until "stop" {state n ->
// print random slash with random color
c = (add 16 (remainder (integer (add n (multiply (random) 100))) 216))
(print (if (integer (add (random) 0.5)) {<- (color c "╱")} {<- (color c "╲")}))
// wait to slow down animation
(wait 0.001)
})
})
will produce the following:
This code:
(use "./colors.crumb" {
(loop 100 {i ->
i = (add i 1)
(if (is (remainder i 15) 0) {
(print (bold (color 88 (background 220 "fizzbuzz"))) "\n")
} {
(if (is (remainder i 3) 0) {
(print (color 36 "fizz") "\n")
} {
(if (is (remainder i 5) 0) {
(print (color "#88ddff" "buzz") "\n")
} {
(print (color (list 255 0 127) (string i)) "\n")
})
})
})
})
})
will produce the following:
This code:
(use "./colors.crumb" {
(print "\e[H\e[2J")
(print "Specify color in ANSI, Hex, RGB:\n")
(print " " (color 208 "208") "\n")
(print " " (color "#dd9900" "#dd9900") "\n")
(print " " (color (list 221 153 0) "(list 221, 153, 0)") "\n\n")
(print "Specify background in ANSI, Hex, RGB:\n")
(print " " (background 208 "208") "\n")
(print " " (background "#dd9900" "#dd9900") "\n")
(print " " (background (list 221 153 0) "(list 221, 153, 0)") "\n\n")
(print "Use both:\n")
(print " " (color "#ddff00" (background 208 "208")) "\n")
(print " " (color "#ddff00" (background "#dd9900" "#dd9900")) "\n")
(print " " (color "#ddff00" (background (list 221 153 0) "(list 221, 153, 0)")) "\n\n")
(print "Use other styling:\n")
(print " " (bold "bold") "\n")
(print " " (faint "faint") "\n")
(print " " (underline "underline") "\n")
(print " " (inverse "inverse") "\n\n")
(print
" "
(background 166 (underline "Crumb:"))
(color "#999999" "a programming language that is")
(bold (color "#ff00dd" "simple,"))
(bold (color "#00ffdd" "not easy."))
"\n\n"
)
})
Will produce the following:
Build:
docker build -t colors.crumb git@github.com:ronilan/colors.crumb.git#main
Run:
docker run --rm -it colors.crumb
Or "all in one":
docker run --rm -it $(docker build -q git@github.com:ronilan/colors.crumb.git#main)
Then in the shell:
./crumb examples/demo.crumb
Clone the repo:
git clone git@github.com:ronilan/colors.crumb.git
CD into directory:
cd colors.crumb
Build Crumb interpreter:
chmod +x build-crumb.sh && ./build-crumb.sh
Run:
./crumb examples/10-print.crumb
-
(color def str)
- Returns
str
wrapped in color escape codes calculated fromdef
. def
can be of a any type. Number is assumed ANSI code. String is assumed HEX value. List is assumed RGB.def
:integer
,string
,list
.str
:string
- Returns
-
(background def str)
- Returns
str
wrapped in background escape codes calculated fromdef
. def
can be of a any type. Number is assumed ANSI code. String is assumed HEX value. List is assumed RGB.def
:integer
,string
,list
.str
:string
- Returns
-
(bold str)
- Returns
str
wrapped in bold escape codes. str
:string
- Returns
-
(underline str)
- Returns
str
wrapped in underline escape codes. str
:string
- Returns
-
(faint str)
- Returns
str
wrapped in faint escape codes. str
:string
- Returns
-
(inverse str)
- Returns
str
wrapped in inverse escape codes. str
:string
- Returns
-
(hex_to_ansi_8 str)
- Returns
integer
ANSI color number most closely matching the HEX color definition. str
:string
- Returns
-
(ansi_8_to_hex num)
- Returns
string
HEX color definition matching the ANSI color number. num
:integer
- Returns
-
(rgb_to_ansi_8 arr)
- Returns
integer
ANSI color number most closely matching the RGB color definition. arr
:list
- Returns
-
(ansi_8_to_rgb num)
- Returns
list
RGB color definition (three numbers between 0 and 255) matching the ANSI color number. num
:integer
- Returns
-
(rgb_to_hex arr)
- Returns
string
HEX color definition matching the RGB color definition. arr
:list
- Returns
-
(hex_to_rgb str)
- Returns
list
RGB color definition (three numbers between 0 and 255) matching the HEX color definition. str
:string
- Returns
-
(base_10_to_16 num)
- Returns a string representation of
num
in base 16. num
:integer
- Returns a string representation of
-
(base_10_to_6 num)
- Returns a string representation of
num
in base 6. num
:integer
- Returns a string representation of
-
(base_10_to_2 num)
- Returns a string representation of
num
in base 2. num
:integer
- Returns a string representation of
-
(base_10_to_other num base)
- Returns a string representation of
num
inbase
.base
must be in range 2 to 16. num
:integer
,base
:integer
- Returns a string representation of
-
(base_16_to_10 str)
- Returns an integer derived from
str
in base 16. str
:string
- Returns an integer derived from
-
(base_other_to_10 str base)
- Returns an integer derived from
str
inbase
.base
must be in range 2 to 16. str
:string
,base
:integer
- Returns an integer derived from
-
(ansi_from_color_definition def)
- ANSI color calculated from
def
. def
can be of a any type. Number is assumed ANSI code. String is assumed HEX value. List is assumed RGB.def
:integer
,string
,list
.str
:string
- ANSI color calculated from
-
(hex_char_to_dec str)
- Returns an integer derived from
str
in base 16. str
:string
- Returns an integer derived from
-
(dec_to_hex_char num)
- Returns a string representation of
num
in base 16. num
:integer
- Returns a string representation of
-
(string_to_list str)
- Returns
list
containing each character of the string. str
:string
- Returns
-
(list_to_string arr)
- Returns
string
a reduction of the array. arr
:list
- Returns
-
(string_reverse str)
- Returns
str
reversed. str
:string
- Returns
-
(string_pad_start str length char)
- Returns
str
of lengthcount
padded at the start withchar
as many times as needed. str
:string
- Returns