Guest User

Untitled

a guest
Jul 21st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'oyster'
  3.  
  4. spec = Oyster.spec do
  5. name "tsp -- Generate 1x1 PNG files with a particular background colour and opacity"
  6.  
  7. description <<-EOS
  8. tsp is a command-line utility for generating 1x1 pixel PNG files with a given
  9. background colour and opacity. Due to the relative lack of support for RGBA
  10. amongst web browsers, it's useful to be able to generate background images
  11. with the requisite properties.
  12.  
  13. This program requires that the ImageMagick command-line tool `convert` be
  14. installed.
  15. EOS
  16.  
  17. string :colour, :default => "ffffff",
  18. :desc => "The colour of the image"
  19.  
  20. float :opacity, :default => 1.0,
  21. :desc => "The opacity of the image"
  22.  
  23. author "Benedict Eastaugh <benedict@eastaugh.net>"
  24.  
  25. copyright <<-EOS
  26. Copyright 2010 Benedict Eastaugh. This program is free software, distributed
  27. under the 3-clause BSD license.
  28. EOS
  29. end
  30.  
  31. begin; opts = spec.parse
  32. rescue Oyster::HelpRendered; exit
  33. end
  34.  
  35. colour = opts[:colour].sub(/^#/, "").slice(0,6)
  36.  
  37. if colour.length == 3
  38. colour = colour * 2
  39. elsif colour.length != 6
  40. puts "Colour must be a 3- or 6-character hexadecimal string. You tried: " + colour
  41. exit
  42. end
  43.  
  44. red = colour[0,2].hex
  45. green = colour[2,2].hex
  46. blue = colour[4,2].hex
  47.  
  48. opacity = opts[:opacity]
  49.  
  50. if opacity > 1.0
  51. puts "Opacity must be a floating-point number less than 1.0. You tried: " + opacity.to_s
  52. end
  53.  
  54. op_pc = (opacity * 100).to_i
  55. op_pc = op_pc < 100 ? "0" + op_pc.to_s : "100"
  56.  
  57. if red && blue && green
  58. `convert -size 1x1 xc:'rgba(#{red},#{green},#{blue},#{opacity})' #{colour}-#{op_pc}.png`
  59. end
Add Comment
Please, Sign In to add comment