Class: Color::RGB

Inherits:
Base
  • Object
show all
Defined in:
lib/quadtone/color/rgb.rb

Instance Attribute Summary

Attributes inherited from Base

#components

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, average, colorspace_name, #eql?, #hash, #initialize, num_components, #to_s, #to_str

Methods included from Math

#deg2rad, #rad2deg

Constructor Details

This class inherits a constructor from Color::Base

Class Method Details

.cgats_fieldsObject



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

def self.cgats_fields
  %w{RGB_R RGB_G RGB_B}
end

.component_namesObject



5
6
7
# File 'lib/quadtone/color/rgb.rb', line 5

def self.component_names
  [:r, :g, :b]
end

.from_cgats(set) ⇒ Object



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

def self.from_cgats(set)
  new(*set.values_at(*cgats_fields).map { |n| n / 100.0 })
end

Instance Method Details

#bObject



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

def b
  @components[2]
end

#gObject



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

def g
  @components[1]
end

#rObject



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

def r
  @components[0]
end

#to_aObject



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

def to_a
  [r, g, b]
end

#to_cgatsObject



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

def to_cgats
  {
    'RGB_R' => r * 100,
    'RGB_G' => g * 100,
    'RGB_B' => b * 100,
  }
end

#to_pixelObject



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

def to_pixel
  Magick::Pixel.new(*to_a.map { |n| n * Magick::QuantumRange })
end

#to_xyzObject



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

def to_xyz
  # after http://www.easyrgb.com/index.php?X=MATH&H=02#text2

  r0, g0, b0 = [r, g, b].map do |n|
    if n > 0.04045
      ((n + 0.055) / 1.055) ** 2.4
    else
      n / 12.92
    end
  end

  r0 *= 100
  g0 *= 100
  b0 *= 100

  # Observer. = 2°, Illuminant = D65

  x = (r0 * 0.4124) + (g0 * 0.3576) + (b0 * 0.1805)
  y = (r0 * 0.2126) + (g0 * 0.7152) + (b0 * 0.0722)
  z = (r0 * 0.0193) + (g0 * 0.1192) + (b0 * 0.9505)

  Color::XYZ.new([x, y, z])
end