#!/usr/bin/python # -*- coding: utf-8 -*- # Copyright © 2013 Jeff Epler # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from math import floor, log from fractions import Fraction as F inch = F(254,10000) bininch = F(254,10000) / 64 mil = inch / 1000 pspt = inch / 72 dpi = inch / 300 mm = F(1,1000) um = mm / 1000 nm = um / 1000 p2 = set(2**i for i in range(63)) def binfract(d, e): """Is e exactly a binary fraction of d?""" r = e / d return r.denominator in p2 def name_color(nm): if nm <= 380: return "UV" if nm <= 450: return "violet" if nm <= 495: return "blue" if nm <= 570: return "green" if nm <= 590: return "yellow" if nm <= 620: return "orange" if nm <= 750: return "red" return "IR" v = [] for i in range(5, 11): for j in range(3): m = F(1,127) * F(1,5)**i * F(1,3)**j x = int(floor(log(m / 3.8e-7, 2))) m = m / F(2)**x y = m * 1000000000 v.append((y, m)) for y, m in sorted(v, reverse=True): print "%9s % 16sm %8.2fnm" % (name_color(y), m, y), if binfract(m, bininch): print "1/64in", if binfract(m, mil): print "mil", elif binfract(m, inch/100): print ".01in", elif binfract(m, inch/10): print ".01in", if binfract(m, pspt): print "pspt", if binfract(m, dpi): print "dpi", if binfract(m, nm): print "nm", elif binfract(m, 5*nm): print "5nm", elif binfract(m, 25*nm): print "25nm", elif binfract(m, um): print "um", elif binfract(m, 5*um): print "5um", elif binfract(m, 25*um): print "25um", elif binfract(m, mm): print "mm", elif binfract(m, 5*mm): print "5mm", elif binfract(m, 25*mm): print "25mm", print