Class: Color::Palette::Gimp

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/color/palette/gimp.rb

Overview

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).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(palette) ⇒ Gimp

Create a new GIMP palette.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/color/palette/gimp.rb', line 43

def initialize(palette)
  @colors   = []
  @names    = {}
  @valid    = false
  @name     = "(unnamed)"

  index     = 0

  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/i
      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[index]  = color
    @names[name] ||= []
    @names[name]  << color

    index += 1
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



106
107
108
# File 'lib/color/palette/gimp.rb', line 106

def name
  @name
end

Class Method Details

.from_file(filename) ⇒ Object

Create a GIMP palette object from the named file.



32
33
34
# File 'lib/color/palette/gimp.rb', line 32

def from_file(filename)
  File.open(filename, "rb") { |io| Color::Palette::Gimp.from_io(io) }
end

.from_io(io) ⇒ Object

Create a GIMP palette object from the provided IO.



37
38
39
# File 'lib/color/palette/gimp.rb', line 37

def from_io(io)
  Color::Palette::Gimp.new(io.read)
end

Instance Method Details

#[](key) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/color/palette/gimp.rb', line 83

def [](key)
  if key.kind_of?(Numeric)
    @colors[key]
  else
    @names[key]
  end
end

#eachObject

Loops through each colour.



92
93
94
# File 'lib/color/palette/gimp.rb', line 92

def each
  @colors.each { |el| yield el }
end

#each_nameObject

Loops through each named colour set.



97
98
99
# File 'lib/color/palette/gimp.rb', line 97

def each_name #:yields color_name, color_set:#
  @names.each { |color_name, color_set| yield color_name, color_set }
end

#valid?Boolean

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/color/palette/gimp.rb', line 102

def valid?
  @valid
end