From 58731c52fbab6aebb917f7ffa8f075a88c707110 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Fri, 21 Jun 2024 15:31:29 +0200 Subject: [PATCH] Add colors-256 script --- home/packages/cli/scripts/default.nix | 1 + .../scripts/packages/colors256/colors-256.sh | 101 ++++++++++++++++++ .../scripts/packages/colors256/default.nix | 9 ++ .../packages/cli/scripts/packages/default.nix | 1 + 4 files changed, 112 insertions(+) create mode 100644 home/packages/cli/scripts/packages/colors256/colors-256.sh create mode 100644 home/packages/cli/scripts/packages/colors256/default.nix diff --git a/home/packages/cli/scripts/default.nix b/home/packages/cli/scripts/default.nix index e54ce9e..127e72a 100644 --- a/home/packages/cli/scripts/default.nix +++ b/home/packages/cli/scripts/default.nix @@ -8,5 +8,6 @@ in { home.packages = with scriptPkgs; [ bitcoin cheatsh + colors256 ]; } diff --git a/home/packages/cli/scripts/packages/colors256/colors-256.sh b/home/packages/cli/scripts/packages/colors256/colors-256.sh new file mode 100644 index 0000000..205fbc0 --- /dev/null +++ b/home/packages/cli/scripts/packages/colors256/colors-256.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash + +# # Print out 256 colors, with each number printed in its corresponding colour +# +# This file is uploaded on +# It was originally intended to be used as an alias that curled this URL and piped it into bash, however +# this is very unsafe as the owner can change the content of this gist at his convenience, meaning it was +# a potential security vulnerability. This is a script version of this alias to avoid this issue. +# +# The copyright for this file belongs to the original author: Tom Hale, 2016 +# This file was released under MIT license + +set -eu # Fail on errors or undeclared variables + +printable_colours=256 + +# Return a colour that contrasts with the given colour +# Bash only does integer division, so keep it integral +function contrast_colour { + colour="$1" + + if ((colour < 16)); then # Initial 16 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 + + # 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 )) + + # If luminance is bright, print number in black, white otherwise. + # 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 +function print_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 +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 { + 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 + 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 # 6x6x6 colour cube between 16 and 231 inclusive +print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey diff --git a/home/packages/cli/scripts/packages/colors256/default.nix b/home/packages/cli/scripts/packages/colors256/default.nix new file mode 100644 index 0000000..ef40b74 --- /dev/null +++ b/home/packages/cli/scripts/packages/colors256/default.nix @@ -0,0 +1,9 @@ +{pkgs, ...}: +(pkgs.writeShellApplication { + name = "colors-256"; + runtimeInputs = with pkgs; [coreutils]; + text = '' + ${builtins.readFile ./colors-256.sh} + ''; +}) + diff --git a/home/packages/cli/scripts/packages/default.nix b/home/packages/cli/scripts/packages/default.nix index 330c74b..b73ab34 100644 --- a/home/packages/cli/scripts/packages/default.nix +++ b/home/packages/cli/scripts/packages/default.nix @@ -5,6 +5,7 @@ packages = { bitcoin = pkgs.callPackage ./bitcoin.nix {}; cheatsh = pkgs.callPackage ./cheatsh {}; + colors256 = pkgs.callPackage ./colors256 {}; }; in packages