Sha256: e2c841e54a73f58f06fbff4a52d3feb099d8424866fa979d48bc04b49b359390

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

#--
# Color
# Colour management with Ruby
# http://rubyforge.org/projects/color
#   Version 1.4.0
#
# Licensed under a MIT-style licence. See Licence.txt in the main
# distribution for full licensing information.
#
# Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
#
# $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
#++

# A colour object representing YIQ (NTSC) colour encoding.
class Color::YIQ
  # Creates a YIQ colour object from fractional values 0 .. 1.
  #
  #   Color::YIQ.new(0.3, 0.2, 0.1)
  def self.from_fraction(y = 0, i = 0, q = 0)
    color = Color::YIQ.new
    color.y = y
    color.i = i
    color.q = q
    color
  end

  # Creates a YIQ colour object from percentages 0 .. 100.
  #
  #   Color::YIQ.new(10, 20, 30)
  def initialize(y = 0, i = 0, q = 0)
    @y = y / 100.0
    @i = i / 100.0
    @q = q / 100.0
  end

  # Compares the other colour to this one. The other colour will be
  # converted to YIQ before comparison, so the comparison between a YIQ
  # colour and a non-YIQ colour will be approximate and based on the other
  # colour's #to_yiq conversion. If there is no #to_yiq conversion, this
  # will raise an exception. This will report that two YIQ values are
  # equivalent if all component colours are within COLOR_TOLERANCE of each
  # other.
  def ==(other)
    other = other.to_yiq
    other.kind_of?(Color::YIQ) and
    ((@y - other.y).abs <= Color::COLOR_TOLERANCE) and
    ((@i - other.i).abs <= Color::COLOR_TOLERANCE) and
    ((@q - other.q).abs <= Color::COLOR_TOLERANCE) 
  end

  def to_yiq
    self
  end

  def brightness
    @y
  end
  def to_grayscale
    Color::GrayScale.new(@y)
  end
  alias to_greyscale to_grayscale

  def y
    @y
  end
  def y=(yy)
    @y = Color.normalize(yy)
  end
  def i
    @i
  end
  def i=(ii)
    @i = Color.normalize(ii)
  end
  def q
    @q
  end
  def q=(qq)
    @q = Color.normalize(qq)
  end

  def inspect
    "YIQ [%.2f%%, %.2f%%, %.2f%%]" % [ @y * 100, @i * 100, @q * 100 ]
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
timocratic-color-1.4.1.0 lib/color/yiq.rb
color-1.4.0 lib/color/yiq.rb
mack-pdf_writer-0.8.2 lib/gems/color-1.4.0/lib/color/yiq.rb
mack-pdf_writer-0.8.3 lib/gems/color-1.4.0/lib/color/yiq.rb
mack-pdf_writer-0.8.3.1 lib/gems/color-1.4.0/lib/color/yiq.rb