Class: Colors::XYZ

Inherits:
AbstractColor show all
Includes:
Helper
Defined in:
lib/colors/xyz.rb

Constant Summary collapse

EPSILON =
(6/29r)**3
KAPPA =
(29/3)**3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractColor

#desaturate

Constructor Details

#initialize(x, y, z) ⇒ XYZ

Returns a new instance of XYZ.



44
45
46
# File 'lib/colors/xyz.rb', line 44

def initialize(x, y, z)
  @x, @y, @z = canonicalize(x, y, z)
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



48
49
50
# File 'lib/colors/xyz.rb', line 48

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



48
49
50
# File 'lib/colors/xyz.rb', line 48

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



48
49
50
# File 'lib/colors/xyz.rb', line 48

def z
  @z
end

Class Method Details

.from_rgb(r, g, b) ⇒ Object



31
32
33
34
# File 'lib/colors/xyz.rb', line 31

def self.from_rgb(r, g, b)
  c = RGB2XYZ.dot(Numo::DFloat[to_linear(r), to_linear(g), to_linear(b)])
  new(c[0], c[1], c[2])
end

.from_xyY(x, y, large_y) ⇒ Object



25
26
27
28
29
# File 'lib/colors/xyz.rb', line 25

def self.from_xyY(x, y, large_y)
  large_x = large_y*x/y
  large_z = large_y*(1 - x - y)/y
  new(large_x, large_y, large_z)
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/colors/xyz.rb', line 54

def ==(other)
  case other
  when XYZ
    x == other.x && y == other.y && z == other.z
  else
    super
  end
end

#componentsObject



50
51
52
# File 'lib/colors/xyz.rb', line 50

def components
  [x, y, z]
end

#luv_components(wp) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/colors/xyz.rb', line 76

def luv_components(wp)
  yy = y/wp.y
  uu, vv = uv_values
  l = if yy <= EPSILON
        KAPPA * yy
      else
        116 * Math.cbrt(yy).to_r - 16
      end
  if l <= 1e-8
    u = v = 0r
  else
    wp_u, wp_v = wp.uv_values
    u = 13*l*(uu - wp_u)
    v = 13*l*(vv - wp_v)
  end
  [l, u, v]
end

#rgb_componentsObject



67
68
69
70
71
72
73
74
# File 'lib/colors/xyz.rb', line 67

def rgb_components
  c = XYZ2RGB.dot(Numo::DFloat[x, y, z])
  [
    srgb_compand(c[0]).clamp(0r, 1r),
    srgb_compand(c[1]).clamp(0r, 1r),
    srgb_compand(c[2]).clamp(0r, 1r)
  ]
end

#to_rgbObject



63
64
65
# File 'lib/colors/xyz.rb', line 63

def to_rgb
  RGB.new(*rgb_components)
end

#uv_valuesObject



94
95
96
97
98
99
100
# File 'lib/colors/xyz.rb', line 94

def uv_values
  d = x + 15*y + 3*z
  return [0r, 0r] if d == 0
  u = 4*x / d
  v = 9*y / d
  [u, v]
end