Class Color::Palette::Gimp
In: lib/color/palette/gimp.rb
Parent: Object
Enumerable Gimp AdobeColor CMYK\n[lib/color.rb\nlib/color/cmyk.rb] GrayScale\n[lib/color.rb\nlib/color/grayscale.rb] RGB\n[lib/color.rb\nlib/color/rgb-colors.rb\nlib/color/rgb.rb] YIQ\n[lib/color.rb\nlib/color/yiq.rb] HSL MonoContrast lib/color/cmyk.rb lib/color/grayscale.rb lib/color/rgb.rb lib/color/yiq.rb lib/color/hsl.rb CSS lib/color/palette/gimp.rb lib/color/palette/adobecolor.rb lib/color/palette/monocontrast.rb Palette Color dot/m_10_0.png

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

Methods

[]   each   each_name   from_file   from_io   new   size   valid?   values_at  

Included Modules

Enumerable

Attributes

name  [R] 

Public Class methods

Create a GIMP palette object from the named file.

[Source]

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

[Source]

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

[Source]

    # 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

Public Instance methods

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.

[Source]

    # 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

Loops through each colour.

[Source]

     # File lib/color/palette/gimp.rb, line 99
 99:   def each
100:     @colors.each { |el| yield el }
101:   end

Loops through each named colour set.

[Source]

     # File lib/color/palette/gimp.rb, line 104
104:   def each_name #:yields color_name, color_set:#
105:     @names.each { |color_name, color_set| yield color_name, color_set }
106:   end

[Source]

     # File lib/color/palette/gimp.rb, line 113
113:   def size
114:     @colors.size
115:   end

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

[Source]

     # File lib/color/palette/gimp.rb, line 109
109:   def valid?
110:     @valid
111:   end

Provides the colour or colours at the provided selectors.

[Source]

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

[Validate]