Class: Color::Base

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/quadtone/color.rb

Direct Known Subclasses

CMYK, DeviceN, Gray, Lab, QTR, RGB, XYZ

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Math

#deg2rad, #rad2deg

Constructor Details

#initialize(arg) ⇒ Base

Returns a new instance of Base.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/quadtone/color.rb', line 39

def initialize(arg)
  components = case arg
  when String
    arg =~ /^(\w+)\((.+)\)$/ or raise "Can't initialize #{self.class}: bad color string: #{arg.inspect}"
    raise "Expected #{self.class.colorspace_name.inspect} but got #{$1.inspect}" if $1.downcase != self.class.colorspace_name
    $2.split(/,\s+/).map(&:to_f)
  when Hash
    self.class.component_names.map { |n| arg[n] }
  when Array
    arg.map(&:to_f)
  else
    raise "Can't initialize #{self.class}: unknown object: #{arg.inspect}"
  end
  raise "Can't initialize #{self.class}: too many components specified: #{components.inspect}" if components.length > self.class.num_components
  @components = [0] * self.class.num_components
  components.each_with_index { |n, i| @components[i] = n if n }
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



7
8
9
# File 'lib/quadtone/color.rb', line 7

def components
  @components
end

Class Method Details

.average(colors) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/quadtone/color.rb', line 29

def self.average(colors)
  avg_components = []
  errors = []
  component_names.each_with_index do |comp, i|
    avg_components << colors.map { |c| c.components[i] }.mean
    errors << colors.map { |c| c.components[i] }.standard_deviation
  end
  [new(avg_components), errors.max]
end

.cgats_fieldsObject

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/quadtone/color.rb', line 13

def self.cgats_fields
  raise NotImplementedError, "\#cgats_fields not implemented in #{self}"
end

.colorspace_nameObject



21
22
23
# File 'lib/quadtone/color.rb', line 21

def self.colorspace_name
  self.to_s.split('::').last.downcase
end

.component_namesObject

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/quadtone/color.rb', line 9

def self.component_names
  raise NotImplementedError, "\#component_names not implemented in #{self}"
end

.from_cgats(set) ⇒ Object



25
26
27
# File 'lib/quadtone/color.rb', line 25

def self.from_cgats(set)
  new(set.values_at(*cgats_fields))
end

.num_componentsObject



17
18
19
# File 'lib/quadtone/color.rb', line 17

def self.num_components
  component_names.length
end

Instance Method Details

#<=>(other) ⇒ Object



77
78
79
# File 'lib/quadtone/color.rb', line 77

def <=>(other)
  @components <=> other.components
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/quadtone/color.rb', line 73

def eql?(other)
  @components == other.components
end

#hashObject



69
70
71
# File 'lib/quadtone/color.rb', line 69

def hash
  @components.hash
end

#to_cgatsObject



65
66
67
# File 'lib/quadtone/color.rb', line 65

def to_cgats
  @components
end

#to_sObject



57
58
59
# File 'lib/quadtone/color.rb', line 57

def to_s
  "#{self.class.colorspace_name}(#{@components.map { |n| '%3.1f' % n }.join(', ')})"
end

#to_strObject



61
62
63
# File 'lib/quadtone/color.rb', line 61

def to_str
  to_s
end