Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Forked from HaleTom/print256colours.sh
Created December 9, 2021 12:56
Show Gist options
  • Save danieldietrich/1606ca62a93c94dfb3b44f81303e744a to your computer and use it in GitHub Desktop.
Save danieldietrich/1606ca62a93c94dfb3b44f81303e744a to your computer and use it in GitHub Desktop.

Revisions

  1. @HaleTom HaleTom revised this gist Sep 11, 2016. 1 changed file with 29 additions and 18 deletions.
    47 changes: 29 additions & 18 deletions print256colours.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,20 @@
    #!/bin/bash

    # Tom Hale, 2016. MIT Licence.
    # Print out 256 colours, with each number printed in its corresponding colour
    # See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163

    set -eu # Fail on errors or undeclared variables

    printable_colours=256

    # Return a colour that contrasts with the colour given
    # Return a colour that contrasts with the given colour
    # Bash only does integer division, so keep it integral
    function contrast_colour {
    local r g b luminance
    colour="$1"

    if (( colour < 16 )); then # ANSI colours
    if (( colour < 16 )); then # Initial 16 ANSI colours
    (( colour == 0 )) && printf "15" || printf "0"
    return
    fi
    @@ -21,20 +25,27 @@ function contrast_colour {
    return
    fi

    # 6x6x6 olour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
    # All other colours:
    # 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
    # See http://stackoverflow.com/a/27165165/5353461
    r=$(( (colour-16) / 36 ))
    g=$(( ((colour-16) % 36) / 6 ))
    b=$(( (colour-16) % 6 ))

    # Calculate percieved brightness
    # See https://www.w3.org/TR/AERT#color-contrast
    # and http://www.itu.int/rec/R-REC-BT.601
    # Luminance is in range 0..5000 as each value is 0..5
    luminance=$(( (r * 299) + (g * 587) + (b * 114) ))
    # r=$(( (colour-16) / 36 ))
    g=$(( ((colour-16) % 36) / 6 ))
    # b=$(( (colour-16) % 6 ))

    # If luminance is bright, print number in black, white otherwise.
    [[ $luminance -ge 2500 ]] && printf "0" || printf "15"
    # Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601
    (( g > 2)) && printf "0" || printf "15"
    return

    # Uncomment the below for more precise luminance calculations

    # # Calculate percieved brightness
    # # See https://www.w3.org/TR/AERT#color-contrast
    # # and http://www.itu.int/rec/R-REC-BT.601
    # # Luminance is in range 0..5000 as each value is 0..5
    # luminance=$(( (r * 299) + (g * 587) + (b * 114) ))
    # (( $luminance > 2500 )) && printf "0" || printf "15"
    }

    # Print a coloured block with the number of that colour
    @@ -57,12 +68,12 @@ function print_run {

    # Print blocks of colours
    function print_blocks {
    start="$1"
    end="$2" # inclusive
    block_cols="$3"
    block_rows="$4"
    blocks_per_line="$5"
    block_length=$((block_cols * block_rows))
    local start="$1" i
    local end="$2" # inclusive
    local block_cols="$3"
    local block_rows="$4"
    local blocks_per_line="$5"
    local block_length=$((block_cols * block_rows))

    # Print sets of blocks
    for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do
  2. @HaleTom HaleTom revised this gist Sep 8, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions print256colours.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/bin/bash

    # Print out 256 colours, with each number printed in its corresponding colour
    # See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163
    printable_colours=256

    # Return a colour that contrasts with the colour given
  3. @HaleTom HaleTom revised this gist Sep 8, 2016. 1 changed file with 41 additions and 6 deletions.
    47 changes: 41 additions & 6 deletions print256colours.sh
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,46 @@
    # Print out 256 colours, with each number printed in its corresponding colour
    printable_colours=256

    # Print a number dressed in its colour
    # Return a colour that contrasts with the colour given
    # Bash only does integer division, so keep it integral
    function contrast_colour {
    local r g b luminance
    colour="$1"

    if (( colour < 16 )); then # ANSI colours
    (( colour == 0 )) && printf "15" || printf "0"
    return
    fi

    # Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
    if (( colour > 231 )); then # Greyscale ramp
    (( colour < 244 )) && printf "15" || printf "0"
    return
    fi

    # 6x6x6 olour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
    # See http://stackoverflow.com/a/27165165/5353461
    r=$(( (colour-16) / 36 ))
    g=$(( ((colour-16) % 36) / 6 ))
    b=$(( (colour-16) % 6 ))

    # Calculate percieved brightness
    # See https://www.w3.org/TR/AERT#color-contrast
    # and http://www.itu.int/rec/R-REC-BT.601
    # Luminance is in range 0..5000 as each value is 0..5
    luminance=$(( (r * 299) + (g * 587) + (b * 114) ))

    # If luminance is bright, print number in black, white otherwise.
    [[ $luminance -ge 2500 ]] && printf "0" || printf "15"
    }

    # Print a coloured block with the number of that colour
    function print_colour {
    # printf "\x1b[38;5;${1}m%3d " "$1"
    printf "\x1b[38;5;%sm%3d" "$1" "$1" # Number in colour on black
    printf "\e[48;5;%sm \e[0m" "$1" # Block of colour
    local colour="$1" contrast
    contrast=$(contrast_colour "$1")
    printf "\e[48;5;%sm" "$colour" # Start block of colour
    printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number
    printf "\e[0m " # Reset colour
    }

    # Starting at $1, print a run of $2 colours
    @@ -16,7 +51,7 @@ function print_run {
    for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do
    print_colour "$i"
    done
    printf " "
    printf " "
    }

    # Print blocks of colours
    @@ -45,5 +80,5 @@ function print_blocks {

    print_run 0 16 # The first 16 colours are spread over the whole spectrum
    printf "\n"
    print_blocks 16 231 6 6 2 # The bulk of colours are between 16 and 231 inclusive
    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
  4. @HaleTom HaleTom revised this gist Sep 7, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions print256colours.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@ printable_colours=256

    # Print a number dressed in its colour
    function print_colour {
    printf "\x1b[38;5;${1}m%3d " "$1"
    # printf "\x1b[38;5;${1}m%3d " "$1"
    printf "\x1b[38;5;%sm%3d" "$1" "$1" # Number in colour on black
    printf "\e[48;5;%sm \e[0m" "$1" # Block of colour
    }

    # Starting at $1, print a run of $2 colours
    @@ -14,7 +16,7 @@ function print_run {
    for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do
    print_colour "$i"
    done
    printf " "
    printf " "
    }

    # Print blocks of colours
    @@ -43,5 +45,5 @@ function print_blocks {

    print_run 0 16 # The first 16 colours are spread over the whole spectrum
    printf "\n"
    print_blocks 16 231 6 6 3 # The bulk of colours are between 16 and 231 inclusive
    print_blocks 16 231 6 6 2 # The bulk of colours are between 16 and 231 inclusive
    print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey
  5. @HaleTom HaleTom created this gist Sep 5, 2016.
    47 changes: 47 additions & 0 deletions print256colours.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash

    # Print out 256 colours, with each number printed in its corresponding colour
    printable_colours=256

    # Print a number dressed in its colour
    function print_colour {
    printf "\x1b[38;5;${1}m%3d " "$1"
    }

    # Starting at $1, print a run of $2 colours
    function print_run {
    local i
    for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do
    print_colour "$i"
    done
    printf " "
    }

    # Print blocks of colours
    function print_blocks {
    start="$1"
    end="$2" # inclusive
    block_cols="$3"
    block_rows="$4"
    blocks_per_line="$5"
    block_length=$((block_cols * block_rows))

    # Print sets of blocks
    for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do
    printf "\n" # Space before each set of blocks
    # For each block row
    for (( row = 0; row < block_rows; row++ )) do
    # Print block columns for all blocks on the line
    for (( block = 0; block < blocks_per_line; block++ )) do
    print_run $(( i + (block * block_length) )) "$block_cols"
    done
    (( i += block_cols )) # Prepare to print the next row
    printf "\n"
    done
    done
    }

    print_run 0 16 # The first 16 colours are spread over the whole spectrum
    printf "\n"
    print_blocks 16 231 6 6 3 # The bulk of colours are between 16 and 231 inclusive
    print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey