| 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 35
35: def from_file(filename)
36: File.open(filename, "rb") { |io| Color::Palette::Gimp.from_io(io) }
37: end
Create a GIMP palette object from the provided IO.
# File lib/color/palette/gimp.rb, line 40
40: def from_io(io)
41: Color::Palette::Gimp.new(io.read)
42: end
Create a new GIMP palette from the palette file as a string.
# File lib/color/palette/gimp.rb, line 46
46: def initialize(palette)
47: @colors = []
48: @names = {}
49: @valid = false
50: @name = "(unnamed)"
51:
52: palette.split($/).each do |line|
53: line.chomp!
54: line.gsub!(/\s*#.*\Z/, '')
55:
56: next if line.empty?
57:
58: if line =~ /\AGIMP Palette\Z/
59: @valid = true
60: next
61: end
62:
63: info = /(\w+):\s(.*$)/.match(line)
64: if info
65: @name = info.captures[1] if info.captures[0] =~ /name/i
66: next
67: end
68:
69: line.gsub!(/^\s+/, '')
70: data = line.split(/\s+/, 4)
71: name = data.pop.strip
72: data.map! { |el| el.to_i }
73:
74: color = Color::RGB.new(*data)
75:
76: @colors << color
77: @names[name] ||= []
78: @names[name] << color
79: end
80: end
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 90
90: def [](key)
91: if key.kind_of?(Numeric)
92: @colors[key]
93: else
94: @names[key]
95: end
96: end
Returns true if this is believed to be a valid GIMP palette.
# File lib/color/palette/gimp.rb, line 109
109: def valid?
110: @valid
111: end