Class: ColorLib::Palette::Gimp

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

Overview

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 from the palette file as a string.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/color_lib/palette/gimp.rb', line 31

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/i
      next
    end

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

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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.from_file(filename) ⇒ Object

Create a GIMP palette object from the named file.



20
21
22
# File 'lib/color_lib/palette/gimp.rb', line 20

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

.from_io(io) ⇒ Object

Create a GIMP palette object from the provided IO.



25
26
27
# File 'lib/color_lib/palette/gimp.rb', line 25

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

Instance Method Details

#[](key) ⇒ Object

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.



75
76
77
78
79
80
81
# File 'lib/color_lib/palette/gimp.rb', line 75

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

#eachObject

Loops through each colour.



84
85
86
# File 'lib/color_lib/palette/gimp.rb', line 84

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

#each_nameObject

Loops through each named colour set.



89
90
91
# File 'lib/color_lib/palette/gimp.rb', line 89

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

#sizeObject



98
99
100
# File 'lib/color_lib/palette/gimp.rb', line 98

def size
  @colors.size
end

#valid?Boolean

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

Returns:

  • (Boolean)


94
95
96
# File 'lib/color_lib/palette/gimp.rb', line 94

def valid?
  @valid
end

#values_at(*selectors) ⇒ Object

Provides the colour or colours at the provided selectors.



68
69
70
# File 'lib/color_lib/palette/gimp.rb', line 68

def values_at(*selectors)
  @colors.values_at(*selectors)
end