Skip to content

Instantly share code, notes, and snippets.

@CouldBeThis
Last active April 16, 2022 13:22
Show Gist options
  • Save CouldBeThis/e3e6f168a00c92f1ca5dc22d87cbf694 to your computer and use it in GitHub Desktop.
Save CouldBeThis/e3e6f168a00c92f1ca5dc22d87cbf694 to your computer and use it in GitHub Desktop.
convert colors from hex to HSL (source = list in text file)

uses pastel to take a text file with a list of colors (in hex) and convert them to HSL. my purpose in this is to replace hardcoded hex color codes in GTK themes with named variables.

this gets less and less useful and finished as it goes on. incomplete and infunctional.

NOTE: the # needs to be removed from the hex code for this to work

basic

cat INPUT.txt | xargs -I {} sh -c "echo \#{}; pastel format hsl  {}" > OUTPUT.txt

INPUT.txt:

ec7c06
f6eded
f7edf2
fbe4bc
ff8d14
ff8f1a

OUTPUT.txt:

#ec7c06
hsl(31, 95.0%, 47.5%)
#f6eded
hsl(0, 33.3%, 94.7%)
#f7edf2
hsl(330, 38.5%, 94.9%)
#fbe4bc
hsl(38, 88.7%, 86.1%)
#ff8d14
hsl(31, 100.0%, 53.9%)
#ff8f1a
hsl(31, 100.0%, 55.1%)

more

cat INPUT.txt | xargs -I {} sh -c "printf '@define-color '; pastel format name {}; printf '_{} '; pastel format hsl {}" > OUTPUT.txt

OUPUT.txt:

@define-color darkorange
_ec7c06 hsl(31, 95.0%, 47.5%)
@define-color snow
_f6eded hsl(0, 33.3%, 94.7%)
@define-color lavenderblush
_f7edf2 hsl(330, 38.5%, 94.9%)
@define-color wheat
_fbe4bc hsl(38, 88.7%, 86.1%)
@define-color darkorange
_ff8d14 hsl(31, 100.0%, 53.9%)
@define-color darkorange
_ff8f1a hsl(31, 100.0%, 55.1%)

use manual find/replace in text editor to replace \n_ with - to get everything onto one line:

@define-color darkorange-ec7c06 hsl(31, 95.0%, 47.5%)
@define-color snow-f6eded hsl(0, 33.3%, 94.7%)
@define-color lavenderblush-f7edf2 hsl(330, 38.5%, 94.9%)
@define-color wheat-fbe4bc hsl(38, 88.7%, 86.1%)
@define-color darkorange-ff8d14 hsl(31, 100.0%, 53.9%)
@define-color darkorange-ff8f1a hsl(31, 100.0%, 55.1%)

generate a script to replace all ocurances of hard coded colors with variable

this creates a potentially destructive script with no safeties (doesn't work? why)

cat INPUT.txt | xargs -I {} sh -c "printf 'sed -r s/#{}/@'; pastel format name {}; printf '_{}/g gtk.css > gtk.css'; echo " > colors-change.sh

as above, it is required to do a find/replace manually, after which the result is:

sed -r s/#ec7c06/@darkorange-ec7c06/g gtk.css > gtk.css
sed -r s/#f6eded/@snow-f6eded/g gtk.css > gtk.css
sed -r s/#f7edf2/@lavenderblush-f7edf2/g gtk.css > gtk.css
sed -r s/#fbe4bc/@wheat-fbe4bc/g gtk.css > gtk.css
sed -r s/#ff8d14/@darkorange-ff8d14/g gtk.css > gtk.css
sed -r s/#ff8f1a/@darkorange-ff8f1a/g gtk.css > gtk.css

manually sort codes to remove close duplicates

this will output to terminal the color info and display the colors so in this way I will manually merge very close colors

cat INPUT.txt  | xargs -I {} sh -c "pastel format hsl {}; printf '#{} '; pastel format name {}"

resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment