Sha256: 81aab69781a732cb0f00494dec97a36a4f51fe093c52306d5618ab3c5c5376ae

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

module Colors
  class XYZ < AbstractColor
    include Helper

    EPSILON = (6/29r)**3

    KAPPA = (29/3r)**3

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

    attr_reader :x, :y, :z

    def components
      [x, y, z]
    end

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

    def to_rgb
      RGB.new(*rgb_components)
    end

    def rgb_components
      Convert.xyz_to_rgb(x, y, z)
    end

    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

    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

    private def canonicalize(x, y, z)
      [
        Rational(x),
        Rational(y),
        Rational(z)
      ]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
red-colors-0.4.0 lib/colors/xyz.rb
red-colors-0.3.0 lib/colors/xyz.rb
red-colors-0.2.0 lib/colors/xyz.rb
red-colors-0.1.3 lib/colors/xyz.rb
red-colors-0.1.2 lib/colors/xyz.rb