Sha256: eeb8f6247e32a95c916bf601ca8b6c91faa47345aa88d5cdd9c36baa6c51870e

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

#--
# Colour management with Ruby.
#
# Copyright 2005 Austin Ziegler
#   http://rubyforge.org/ruby-pdf/
#++

require 'color/rgb'

module Color
    # An CMYK colour object.
  class CMYK
    PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"

    def ==(other)
      other = other.to_cmyk if other.kind_of?(Color::RGB)
      other.kind_of?(Color::CMYK) and
      (@c == other.c) and
      (@m == other.m) and
      (@y == other.y) and
      (@k == other.k)
    end

      # Creates a CMYK colour object from fractional values 0 .. 1.
    def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
      colour = Color::CMYK.new
      colour.instance_variable_set(:@c, c)
      colour.instance_variable_set(:@m, m)
      colour.instance_variable_set(:@y, y)
      colour.instance_variable_set(:@k, k)
      colour
    end

      # Creates a CMYK colour object from percentages.
    def initialize(c = 0, m = 0, y = 0, k = 0)
      @c = c / 100.0
      @m = m / 100.0
      @y = y / 100.0
      @k = k / 100.0
    end

      # Present the colour as a fill colour string for PDF.
    def pdf_fill
      PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
    end

      # Present the colour as a stroke colour string for PDF.
    def pdf_stroke
      PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
    end

      # Present the colour as an HTML/CSS colour string.
    def html
      to_rgb.html
    end

      # Convert the CMYK colour to RGB. Note that colour experts strongly
      # suggest that this is a *bad* idea, as CMYK represents percentages of
      # inks, not mixed colour intensities like RGB.
    def to_rgb
      r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f)
      g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f)
      b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f)
      Color::RGB.from_fraction(r, g, b)
    end

    def to_cmyk
      self.dup
    end

    attr_accessor :c, :m, :y, :k
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
color-tools-1.0.0 lib/color/cmyk.rb
color-tools-1.1.0 lib/color/cmyk.rb