Created
July 26, 2010 15:12
-
-
Save beastaugh/490676 to your computer and use it in GitHub Desktop.
Generate 1x1 PNG files with a particular background colour and opacity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'oyster' | |
spec = Oyster.spec do | |
name "tsp -- Generate 1x1 PNG files with a particular background colour and opacity" | |
description <<-EOS | |
tsp is a command-line utility for generating 1x1 pixel PNG files with a given | |
background colour and opacity. Due to the relative lack of support for RGBA | |
amongst web browsers, it's useful to be able to generate background images | |
with the requisite properties. | |
This program requires that the ImageMagick command-line tool `convert` be | |
installed. | |
EOS | |
string :colour, :default => "ffffff", | |
:desc => "The colour of the image" | |
float :opacity, :default => 1.0, | |
:desc => "The opacity of the image" | |
author "Benedict Eastaugh <[email protected]>" | |
copyright <<-EOS | |
Copyright 2010 Benedict Eastaugh. This program is free software, distributed | |
under the 3-clause BSD license. | |
EOS | |
end | |
begin; opts = spec.parse | |
rescue Oyster::HelpRendered; exit | |
end | |
colour = opts[:colour].sub(/^#/, "").slice(0,6) | |
if colour.length == 3 | |
colour = colour * 2 | |
elsif colour.length != 6 | |
puts "Colour must be a 3- or 6-character hexadecimal string. You tried: " + colour | |
exit | |
end | |
red = colour[0,2].hex | |
green = colour[2,2].hex | |
blue = colour[4,2].hex | |
opacity = opts[:opacity] | |
if opacity > 1.0 | |
puts "Opacity must be a floating-point number less than 1.0. You tried: " + opacity.to_s | |
end | |
op_pc = (opacity * 100).to_i | |
op_pc = op_pc < 100 ? "0" + op_pc.to_s : "100" | |
if red && blue && green | |
`convert -size 1x1 xc:'rgba(#{red},#{green},#{blue},#{opacity})' #{colour}-#{op_pc}.png` | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment