colorscheme: Guess wether a color is dark or light - scripts - various script and utils
HTML git clone git://z3bra.org/scripts
DIR Log
DIR Files
DIR Refs
---
DIR commit 728ebcf1e66d874a29bfb13e438bf70822d646e7
DIR parent 29f9633eee979478a582cef755f7d8af9bc69155
HTML Author: Willy Goiffon <dev@z3bra.org>
Date: Sat, 29 Aug 2026 08:10:50 +0000
colorscheme: Guess wether a color is dark or light
Diffstat:
A colorscheme | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+), 0 deletions(-)
---
DIR diff --git a/colorscheme b/colorscheme
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# Compute perceived brightness of an #RGB color
+# https://en.wikipedia.org/wiki/Rec._709#The_Y'C'BC'R_color_space
+
+usage() {
+ echo "usage: $(basename $0) #RRGGBB" >&2
+}
+
+while getopts "h" OPT; do
+ case $OPT in
+ h) usage; exit 0;;
+ *) usage; exit 1;;
+ esac
+done
+
+hex=${1#\#}
+r="0x$(echo $hex|cut -b1-2)"
+g="0x$(echo $hex|cut -b3-4)"
+b="0x$(echo $hex|cut -b5-6)"
+l=$(printf '%d*0.2126 + %d*0.7152 + %d*0.0722\n' $r $g $b|bc|cut -d. -f1)
+[ $l -gt 128 ] && echo 'light' || echo 'dark'