-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase16-builder
More file actions
executable file
·130 lines (102 loc) · 2.82 KB
/
base16-builder
File metadata and controls
executable file
·130 lines (102 loc) · 2.82 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail
function :printf () {
# shellcheck disable=SC2059
printf -- "$1" "${@:2}"
}
function :printfln () {
# shellcheck disable=SC2059
printf -- "${1}\n" "${@:2}"
}
function :vprintf () {
# shellcheck disable=SC2059
printf -v "$1" -- "$2" "${@:3}"
}
function :println () {
:printfln '%s' "$@"
}
function :log () {
:println "$@" 1>&2
}
function :die () {
:log "$@"
exit 1
}
function :bc () {
:printfln "$1" "${@:2}" | bc --mathlib
}
function :hex:rgb () {
:bc 'ibase=16; %s' "$1"
}
function :rgb:dec () {
:bc 'scale=2; %d/255' "$1" | awk '{printf "%0.2f\n", $0}'
}
if ! read -t 0 || [[ ! "${1:+"x"}" ]]
then :die "Usage: cat scheme.yaml | ${0} template.tmpl"
elif [[ ! -r "$1" ]]
then :die "Error: Unable to read template file ${1}"
fi
readonly templateFile="$1"
declare -A schemeData=()
while IFS= read -r line || [[ "${line:+"x"}" ]]
do
if
: '^(scheme|author|base0[0-9a-fA-F])[[:space:]]*:[[:space:]]*(.+)'
[[ "$line" =~ $_ ]]
then
key="${BASH_REMATCH[1]}" value="${BASH_REMATCH[2]}"
if
: '"(.+)"'
[[ "$value" =~ $_ ]]
then value="${BASH_REMATCH[1]}"
elif
: "'"'(.+)'"'"
[[ "$value" =~ $_ ]]
then value="${BASH_REMATCH[1]}"
fi
schemeData["$key"]="$value"
fi
done
declare -A replacements=()
replacements["scheme-name"]="${schemeData["scheme"]:-"Unknown"}"
replacements["scheme-author"]="${schemeData["author"]:-"Unknown"}"
: "${replacements["scheme-name"],,}"
replacements["scheme-slug"]="${_//" "/"-"}"
for i in {0..15}
do
:vprintf key 'base%02X' "$i"
if [[ ! "${schemeData["$key"]:+"x"}" ]]
then :die "Error: ${key} not found"
fi
if
: '#?([0-9a-fA-F]{6})'
[[ ! "${schemeData["$key"]}" =~ $_ ]]
then :die "Error: Invalid hex color: ${schemeData["$key"]}"
fi
: "${BASH_REMATCH[1]^^}"
hexR="${_:0:2}" hexG="${_:2:2}" hexB="${_:4:2}"
rgbR="$(:hex:rgb "$hexR")" rgbG="$(:hex:rgb "$hexG")" rgbB="$(:hex:rgb "$hexB")"
decR="$(:rgb:dec "$rgbR")" decG="$(:rgb:dec "$rgbG")" decB="$(:rgb:dec "$rgbB")"
replacements["${key}-hex"]="${hexR}${hexG}${hexB}"
replacements["${key}-hex-bgr"]="${hexB}${hexG}${hexR}"
replacements["${key}-hex-r"]="$hexR"
replacements["${key}-hex-g"]="$hexG"
replacements["${key}-hex-b"]="$hexB"
replacements["${key}-rgb-r"]="$rgbR"
replacements["${key}-rgb-g"]="$rgbG"
replacements["${key}-rgb-b"]="$rgbB"
replacements["${key}-dec-r"]="$decR"
replacements["${key}-dec-g"]="$decG"
replacements["${key}-dec-b"]="$decB"
# TODO
replacements["${key}-hsl-h"]=
replacements["${key}-hsl-s"]=
replacements["${key}-hsl-l"]=
done
declare -a sedOpts=()
for search in "${!replacements[@]}"
do
: "${replacements["$search"]}"
sedOpts+=(-e "s/{{${search}}}/${_//\//\\\/}/")
done
sed "${sedOpts[@]}" "$templateFile"