Skip to content

Commit

Permalink
porting more coloring scripts (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdncred committed Mar 9, 2022
1 parent 70c0b6e commit 400b985
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
33 changes: 33 additions & 0 deletions coloring/24bit-1.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let term_cols = ((term size).columns - 1)

# let's itertate through each of the columns of our terminal
for col in 0..$term_cols {
let r = (255 - ($col * 255 / $term_cols) | math round)
let g = ($col * 510 / $term_cols | math round)
let b = ($col * 255 / $term_cols | math round)
if $g > 255 {
let g = (510 - $g)
build-colorstr $r $g $b $col
} else {
build-colorstr $r $g $b $col
}
} | str collect

def build-colorstr [
r:int # Red
g:int # Green
b:int # Blue
c:int # Column
] {
# Heavy use of string interpolation below
let bg = $"(ansi rgb_bg)($r);($g);($b)m"
let fg = $"(ansi rgb_fg)(255 - $r);(255 - $g);(255 - $b)m"
let idx = ($c mod 2)
let slash_str = (if $idx == 0 {
$'/(ansi reset)'
} else {
$'\(ansi reset)'
})
$"($bg)($fg)($slash_str)"
sleep 10ms | ignore
}
75 changes: 75 additions & 0 deletions coloring/256_color_testpattern.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
def contrast_colour [ colour:int ] {
# The first 16 colors
if $colour < 16 {
if $colour == 0 {
15
} else {
0
}
} else {
# The gray colors
if $colour > 231 {
if $colour < 244 {
15
} else {
0
}
} else {
# The rest
let r = ($colour - 16) / 36
let g = (($colour - 16) mod 36) / 6
let b = ($colour - 16) mod 6

# if $g > 2 {
# 0
# } {
# 15
# }

let luminance = ($r * 299) + ($g * 587) + ($b * 114)
if $luminance > 2500 {
0
} else {
15
}
}
}
}

def print_colour [ colour:int ] {
let contrast = (contrast_colour $colour)
let bg_color = $"(ansi idx_bg)($colour)m" # Start block of colour
let fg_color = $"(ansi idx_fg)($contrast)m" # In contrast, print number
let text = $"($colour | into string | str lpad -c ' ' -l 3)(ansi reset)"
$bg_color + $fg_color + $text + " "
}

let printable_colours = 256

def print_run [start:int, amount:int] {
for i in $start..<($start + $amount) {
if $i < $printable_colours {
print_colour $i
} else {
""
}
} | append " " | str collect
}

def print_blocks [start:int, end:int, block-cols:int, block-rows:int, blocks-per-line:int] {
let block-length = ($block-cols * $block-rows)
let end = (($end - $start) / (($blocks-per-line) * $block-length))
for i in 0..<$end {
for row in 0..<$block-rows {
for block in 0..<$blocks-per-line {
print_run ($start + $block * $block-length + $row * $block-cols + $i * $block-length * $blocks-per-line) $block-cols
} | append (char nl) | str collect
} | str collect
# char nl
} | str collect
}

print_run 0 16 # The first 16 colours are spread over the whole spectrum
char nl
print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive
print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey

0 comments on commit 400b985

Please sign in to comment.