| Class | Color::Palette::Gimp |
| In: |
lib/color/palette/gimp.rb
|
| Parent: | Object |
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).
| name | [R] |
Create a GIMP palette object from the named file.
# File lib/color/palette/gimp.rb, line 32
32: def from_file(filename)
33: File.open(filename, "rb") { |io| Color::Palette::Gimp.from_io(io) }
34: end
Create a GIMP palette object from the provided IO.
# File lib/color/palette/gimp.rb, line 37
37: def from_io(io)
38: Color::Palette::Gimp.new(io.read)
39: end
Create a new GIMP palette.
# File lib/color/palette/gimp.rb, line 43
43: def initialize(palette)
44: @colors = []
45: @names = {}
46: @valid = false
47: @name = "(unnamed)"
48:
49: index = 0
50:
51: palette.split($/).each do |line|
52: line.chomp!
53: line.gsub!(/\s*#.*\Z/, '')
54:
55: next if line.empty?
56:
57: if line =~ /\AGIMP Palette\Z/
58: @valid = true
59: next
60: end
61:
62: info = /(\w+):\s(.*$)/.match(line)
63: if info
64: @name = info.captures[1] if info.captures[0] =~ /name/i
65: next
66: end
67:
68: line.gsub!(/^\s+/, '')
69: data = line.split(/\s+/, 4)
70: name = data.pop.strip
71: data.map! { |el| el.to_i }
72:
73: color = Color::RGB.new(*data)
74:
75: @colors[index] = color
76: @names[name] ||= []
77: @names[name] << color
78:
79: index += 1
80: end
81: end
# File lib/color/palette/gimp.rb, line 83
83: def [](key)
84: if key.kind_of?(Numeric)
85: @colors[key]
86: else
87: @names[key]
88: end
89: end