Created
February 6, 2010 12:42
-
-
Save melborne/296691 to your computer and use it in GitHub Desktop.
Graphviz Sampler
This file contains 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
# -*- encoding:utf-8 -*- | |
require "graphaz" | |
COLORS = DATA.readlines.map { |line| line.sub(/\[.*$/, '').chomp } | |
DARKS = %w(black urlywood navyblue navy indigo darkslateblue mediumblue midnightblue blue darkslategray) | |
def init(ga, num) | |
1.upto(num) { |n| ga.add(n.to_s) } | |
1.upto(num) { |n| ga.add("#{rand(num)+1} => #{n}") } | |
ga | |
end | |
def colored100 | |
num = 100 | |
ga = init(GraphAz.new, num) | |
1.upto(num) do |n| | |
color = COLORS.sample | |
ga.node(n.to_s, :style => 'filled', :fillcolor => color) | |
ga.node(n.to_s, :fontcolor => 'white') if DARKS.include?(color) | |
end | |
ga.print_graph(:png => 'rand.png') | |
end | |
def prime_find(num) | |
require "mathn" | |
ga = init(GraphAz.new, num) | |
ga.lap | |
Prime.each do |prime| | |
break if prime > num | |
color = COLORS.sample | |
opt = {:style => 'filled', :fillcolor => color} | |
opt.merge!({:fontcolor => 'white'}) if DARKS.include?(color) | |
ga.node(prime.to_s, opt) | |
ga.lap | |
end | |
3.times { ga.lap } | |
1.upto(num) do |n| | |
color = COLORS.sample | |
opt = {:style => 'filled', :fillcolor => color} | |
opt.merge!({:fontcolor => 'white'}) if DARKS.include?(color) | |
ga.node(n.to_s, opt) | |
end | |
5.times { ga.lap } | |
ga.write | |
end | |
class Fib | |
@@next = 0 | |
def self.next | |
@@next += 1 | |
fib(@@next) | |
end | |
def self.fib(n, a=1, b=0) | |
if n == 0 | |
0 | |
elsif n == 1 | |
a | |
else | |
self.fib(n-1, a+b, a) | |
end | |
end | |
end | |
def fib(limit) | |
ga = GraphAz.new | |
nodes = [] | |
limit.times do |n| | |
nodes << (f = Fib.next) | |
ga.add(f.to_s) | |
end | |
nodes.each { |node| ga.add("#{nodes.sample} => #{node}") } | |
ga.lap | |
nodes.each do |node| | |
color = COLORS.sample | |
opt = {:style => 'filled', :fillcolor => color} | |
opt.merge!({:fontcolor => 'white'}) if DARKS.include?(color) | |
ga.node(node.to_s, opt) | |
ga.lap | |
end | |
ga.lap | |
ga.write | |
end | |
#colored100 | |
#prime_find(100) | |
fib(50) | |
__END__ | |
antiquewhite[1-4] | |
azure[1-4] | |
bisque[1-4] | |
blanchedalmond | |
cornsilk[1-4] | |
floralwhite | |
gainsboro | |
ghostwhite | |
honeydew[1-4] | |
ivory[1-4] | |
lavender | |
lavenderblush[1-4] | |
lemonchiffon[1-4] | |
linen | |
mintcream | |
mistyrose[1-4] | |
moccasin | |
navajowhite[1-4] | |
oldlace | |
papayawhip | |
peachpuff[1-4] | |
seashell[1-4] | |
snow[1-4] | |
thistle[1-4] | |
wheat[1-4] | |
white | |
whitesmoke | |
darkslategray[1-4] | |
dimgray | |
gray[0-100] | |
lightgray | |
lightslategray | |
slategray[1-4] | |
black | |
coral[1-4] | |
crimson | |
darksalmon | |
deeppink[1-4] | |
firebrick[1-4] | |
hotpink[1-4] | |
indianred[1-4] | |
lightpink[1-4] | |
lightsalmon[1-4] | |
maroon[1-4] | |
pink[1-4] | |
red[1-4] | |
salmon[1-4] | |
tomato[1-4] | |
beige | |
brown[1-4] | |
urlywood[1-4] | |
chocolate[1-4] | |
darkkhaki | |
khaki[1-4] | |
peru | |
rosybrown[1-4] | |
saddlebrown | |
sandybrown | |
sienna[1-4] | |
tan[1-4] | |
darkorange[1-4] | |
orange[1-4] | |
orangered[1-4] | |
darkgoldenrod[1-4] | |
gold[1-4] | |
goldenrod[1-4] | |
greenyellow | |
lightgoldenrod[1-4] | |
lightgoldenrodyellow | |
lightyellow[1-4] | |
palegoldenrod | |
yellow[1-4] | |
yellowgreen | |
chartreuse[1-4] | |
darkgreen | |
darkolivegreen[1-4] | |
darkseagreen[1-4] | |
forestgreen | |
green[1-4] | |
lawngreen | |
lightseagreen | |
limegreen | |
mediumseagreen | |
mediumspringgreen | |
olivedrab[1-4] | |
palegreen[1-4] | |
seagreen[1-4] | |
springgreen[1-4] | |
mediumorchid[1-4] | |
aquamarine[1-4] | |
cyan[1-4] | |
darkturquoise | |
lightcyan[1-4] | |
mediumaquamarine | |
mediumturquoise | |
paleturquoise[1-4] | |
turquoise[1-4] | |
aliceblue | |
blue[1-4] | |
cadetblue[1-4] | |
cornflowerblue | |
darkslateblue | |
deepskyblue[1-4] | |
dodgerblue[1-4] | |
indigo | |
lightblue[1-4] | |
lightskyblue[1-4] | |
lightslateblue[1-4] | |
mediumblue | |
mediumslateblue | |
midnightblue | |
navy | |
navyblue | |
powderblue | |
royalblue[1-4] | |
skyblue[1-4] | |
slateblue[1-4] | |
steelblue[1-4] | |
blueviolet | |
darkorchid[1-4] | |
darkviolet | |
magenta[1-4] | |
mediumpurple[1-4] | |
mediumvioletred | |
orchid[1-4] | |
palevioletred[1-4] | |
plum[1-4] | |
purple[1-4] | |
violet | |
violetred[1-4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment