File: darker.sh

package info (click to toggle)
arc-theme 20190213-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,128 kB
  • sloc: xml: 2,223; makefile: 348; sh: 344
file content (33 lines) | stat: -rwxr-xr-x 661 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env bash
set -ueo pipefail
#set -x

darker_channel() {
  value="$1"
  light_delta="$2"
  result="$(bc <<< "ibase=16; $value - $light_delta")"
  if [[ "$result" -lt 0 ]]; then
    result=0
  fi
  if [[ "$result" -gt 255 ]]; then
    result=255
  fi
  echo "$result"
}

darker() {
  hexinput="$(tr '[:lower:]' '[:upper:]' <<< "$1")"
  light_delta="${2-10}"

  a="$(cut -c-2 <<< "$hexinput")"
  b="$(cut -c3-4 <<< "$hexinput")"
  c="$(cut -c5-6 <<< "$hexinput")"

  r="$(darker_channel "$a" "$light_delta")"
  g="$(darker_channel "$b" "$light_delta")"
  b="$(darker_channel "$c" "$light_delta")"

  printf '%02x%02x%02x\n' "$r" "$g" "$b"
}

darker "$@"