Skip to content

Instantly share code, notes, and snippets.

@adion
Created February 19, 2013 17:14
Show Gist options
  • Save adion/4987853 to your computer and use it in GitHub Desktop.
Save adion/4987853 to your computer and use it in GitHub Desktop.
A simple shell script used for converting pixel units to relative ems. It assumes a baseline of 16px if one isn't provided.
#!/bin/bash
baseline=16 # px
em=0
px=0
float_scale=3 # floating point precision for bc
function px2em()
{
local result=$(echo "scale=$float_scale; $px / $baseline" | bc -q 2>/dev/null)
result=$(printf '%*.*f' 0 "$float_scale" "$result")
echo $result
}
# we need at least one argument
if [ $# -lt 1 ]
then
echo "Usage: `basename $0` [baseline] pixel-value"
exit 85
fi
if [ $# -eq 2 ]
then
baseline=$1
px=$2
else
px=$1
fi
px2em
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment