Parent

Included Modules

Color::Palette::Gimp

A class that can read a GIMP (GNU Image Manipulation Program) palette file and provide a Hash-like interface to the contents. GIMP colour palettes are RGB values only.

Because two or more entries in a GIMP palette may have the same name, all named entries are returned as an array.

pal = Color::Palette::Gimp.from_file(my_gimp_palette)
pal[0]          => Color::RGB<...>
pal["white"]    => [ Color::RGB<...> ]
pal["unknown"]  => [ Color::RGB<...>, Color::RGB<...>, ... ]

GIMP Palettes are always indexable by insertion order (an integer key).

Attributes

name[R]

Public Class Methods

from_file(filename) click to toggle source

Create a GIMP palette object from the named file.

# File lib/color/palette/gimp.rb, line 21
def from_file(filename)
  File.open(filename, "rb") { |io| Color::Palette::Gimp.from_io(io) }
end
from_io(io) click to toggle source

Create a GIMP palette object from the provided IO.

# File lib/color/palette/gimp.rb, line 26
def from_io(io)
  Color::Palette::Gimp.new(io.read)
end
new(palette) click to toggle source

Create a new GIMP palette from the palette file as a string.

# File lib/color/palette/gimp.rb, line 32
def initialize(palette)
  @colors   = []
  @names    = {}
  @valid    = false
  @name     = "(unnamed)"

  palette.split($/).each do |line|
    line.chomp!
    line.gsub!(/\s*#.*\Z/, '')

    next if line.empty?

    if line =~ /\AGIMP Palette\Z/
      @valid = true
      next
    end

    info = /(\w+):\s(.*$)/.match(line)
    if info
      @name = info.captures[1] if info.captures[0] =~ /name/
      next
    end

    line.gsub!(/^\s+/, '')
    data = line.split(/\s+/, 4)
    name = data.pop.strip
    data.map! { |el| el.to_i }

    color = Color::RGB.new(*data)

    @colors << color
    @names[name] ||= []
    @names[name]  << color
  end
end

Public Instance Methods

[](key) click to toggle source

If a Numeric key is provided, the single colour value at that position will be returned. If a String key is provided, the colour set (an array) for that colour name will be returned.

# File lib/color/palette/gimp.rb, line 76
def [](key)
  if key.kind_of?(Numeric)
    @colors[key]
  else
    @names[key]
  end
end
each() click to toggle source

Loops through each colour.

# File lib/color/palette/gimp.rb, line 85
def each
  @colors.each { |el| yield el }
end
each_name() click to toggle source

Loops through each named colour set.

# File lib/color/palette/gimp.rb, line 90
def each_name #:yields color_name, color_set:#
  @names.each { |color_name, color_set| yield color_name, color_set }
end
size() click to toggle source
# File lib/color/palette/gimp.rb, line 99
def size
  @colors.size
end
valid?() click to toggle source

Returns true if this is believed to be a valid GIMP palette.

# File lib/color/palette/gimp.rb, line 95
def valid?
  @valid
end
values_at(*selectors) click to toggle source

Provides the colour or colours at the provided selectors.

# File lib/color/palette/gimp.rb, line 69
def values_at(*selectors)
  @colors.values_at(*selectors)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.