#!/usr/local/bin/ruby -w
require 'RMagick'

Compliance = {Magick::SVGCompliance => 'SVG',
              Magick::X11Compliance => 'X11',
              Magick::XPMCompliance => 'XPM',
              Magick::AllCompliance => 'All'}

colors = Magick::ImageList.new

# Add a row of "null" colors so we'll have
# room to add the title at the top.
4.times { colors.read('null:') }

# Create a 200x25 image for each named color.
# Label with the name, RGB values, and compliance type.
Magick::colors { |c|
    if c.name !~ /gray/ then    # omit SVG 'grays'
        colors.new_image(200, 25) {
            self.background_color = c.color
            self.border_color = 'gray50'
            }
        rgb  = sprintf('#%02x%02x%02x', c.color.red&0xff,  c.color.green&0xff, c.color.blue&0xff)
        rgb += sprintf('%02x', c.color.opacity&0xff) if c.color.opacity != 0
        colors.cur_image['Label'] = "#{c.name} (#{rgb}) #{Compliance[c.compliance]}"
    end
}

# Keep track of how far along the montage is.
monitor = Proc.new { |text, quantum, span|
    printf("%s %3.1f%% complete\n", text, ((quantum/span.to_f)*100.0))
    }
Magick.set_monitor(monitor)

# Montage. Each image will have 40 tiles.
# There will be 16 images.
montage = colors.montage {
    self.geometry = '200x25+10+5'
    self.gravity = Magick::CenterGravity
    self.tile = '4x10'
    self.background_color = 'black'
    self.border_width = 1
    self.fill = 'white'
    self.stroke = 'transparent'
}

Magick.set_monitor(nil)

# Add the title at the top, over the 'null:'
# tiles we added at the very beginning.
title = Magick::Draw.new
title.annotate(montage, 0,0,0,40, 'Named Colors') {
    self.font_family = 'Helvetica'
    self.fill = 'white'
    self.stroke = 'transparent'
    self.pointsize = 32
    self.font_weight = Magick::BoldWeight
    self.gravity = Magick::NorthGravity
}

montage.each { |f| f.compression = Magick::ZipCompression }
montage.write('colors.miff')

# Make a small sample of the full montage to display in the HTML file.
sample = montage[9].crop(55, 325, 495, 110)
sample.write('colors.gif')
exit
