Skip to content

Instantly share code, notes, and snippets.

@orls
Forked from beastaugh/tsp.rb
Created February 3, 2011 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orls/809408 to your computer and use it in GitHub Desktop.
Save orls/809408 to your computer and use it in GitHub Desktop.
#!/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 <benedict@eastaugh.net>"
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