add color alpha converter
This commit is contained in:
		
							parent
							
								
									13c54e54ae
								
							
						
					
					
						commit
						d2a47365d4
					
				
					 2 changed files with 56 additions and 0 deletions
				
			
		
							
								
								
									
										47
									
								
								color-alpha-converter/cac.clj
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										47
									
								
								color-alpha-converter/cac.clj
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -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)))
 | 
			
		||||
							
								
								
									
										9
									
								
								color-alpha-converter/readme.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								color-alpha-converter/readme.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
# 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
 | 
			
		||||
./cac.clj 50
 | 
			
		||||
./cac.clj 80
 | 
			
		||||
./cac.clj -i ff
 | 
			
		||||
./cac.clj -i bf
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue