From b141bdadc375e9e9002c81c7550a6134fea2cd7e Mon Sep 17 00:00:00 2001 From: Florian Schrofner Date: Sat, 30 Apr 2022 22:32:50 +0200 Subject: [PATCH] add color alpha converter --- color-alpha-converter/cac.clj | 47 +++++++++++++++++++++++++++++++++ color-alpha-converter/readme.md | 11 ++++++++ 2 files changed, 58 insertions(+) create mode 100755 color-alpha-converter/cac.clj create mode 100644 color-alpha-converter/readme.md diff --git a/color-alpha-converter/cac.clj b/color-alpha-converter/cac.clj new file mode 100755 index 0000000..be3c6c0 --- /dev/null +++ b/color-alpha-converter/cac.clj @@ -0,0 +1,47 @@ +#!/usr/bin/env bb + +(require '[clojure.tools.cli :refer [parse-opts]]) +(use '[clojure.string]) +(require '[clojure.math :refer [round]]) + +(def cli-options + [["-i" "--inverse" "Inverses the conversion so the value is converted to a percentage from a given hex number." + :id :inverse + :default false]]) + +(def step-size 2.55M) +(def parsed-params (parse-opts *command-line-args* cli-options)) +(def options (:options parsed-params)) +(def value (first (:arguments parsed-params))) + +(defn pad-with-zero + [length string] + (as-> string $ + (count $) + (- length $) + (take $ (repeat "0")) + (concat $ string) + (join $))) + +(defn to-hex + [value] + (->> value + (Integer/parseInt) + (* step-size) + (round) + (int) + (format "%x") + (pad-with-zero 2))) + +(defn from-hex + [value] + (as-> value $ + (Integer/parseInt $ 16) + (with-precision 2(/ $ step-size)) + (round $) + (int $) + (str $))) + +(if (:inverse options) + (print (from-hex value)) + (print (to-hex value))) diff --git a/color-alpha-converter/readme.md b/color-alpha-converter/readme.md new file mode 100644 index 0000000..5e6c64c --- /dev/null +++ b/color-alpha-converter/readme.md @@ -0,0 +1,11 @@ +# Color Alpha Converter +Converts the given alpha in percent to a hex value usable by Android. +You can use the `-i` flag to inverse the conversion and get the alpha percentage for a hex value. + +## Example Usage +```bash +./cac.clj 50 +./cac.clj 80 +./cac.clj -i ff +./cac.clj -i bf +```