Module: Color

### Home Classes Methods

### In Files

- lib/color.rb

### Namespace

- MODULE Color::CSS
- MODULE Color::Palette
- CLASS Color::CMYK
- CLASS Color::GrayScale
- CLASS Color::HSL
- CLASS Color::RGB
- CLASS Color::YIQ

### Methods

- ::coerce
- ::equivalent?
- ::near?
- ::near_one?
- ::near_one_or_more?
- ::near_zero?
- ::near_zero_or_less?
- ::new
- ::normalize
- ::normalize_16bit
- ::normalize_8bit
- ::normalize_byte
- ::normalize_fractional
- ::normalize_to_range
- ::normalize_word
- #==
- #name
- #names

### Files

- Manifest.txt

### Class/Module Index

Quicksearch

- Color
- Color::CMYK
- Color::CSS
- Color::GrayScale
- Color::GrayScale
- Color::HSL
- Color::Palette
- Color::Palette::AdobeColor
- Color::Palette::Gimp
- Color::Palette::MonoContrast
- Color::RGB
- Color::RGB::Metallic
- Color::YIQ
- Object
- Object::GreyScale

No matching classes.

# Color

# Colour Management with Ruby

### Constants

COLOR_EPSILON

  The maximum "resolution" for colour math; if any value is less than or equal to this value, it is treated as zero.

COLOR_TOLERANCE

  The tolerance for comparing the components of two colours. In general, colours are considered equal if all of their components are within this tolerance value of each other.

COLOR_VERSION

### Public Class Methods

coerce(a, b) click to toggle source

Coerces, if possible, the second given colour object to the first given colour object type. This will probably involve colour conversion and therefore a loss of fidelity.

    # File lib/color.rb, line 91
    def coerce(a, b)
      a.coerce(b)
    end

equivalent?(a, b) click to toggle source

Returns `true` if the two colours are roughly equivalent. If colour conversions are required, this all conversions will be implemented using the default conversion mechanism.

    # File lib/color.rb, line 84
    def equivalent?(a, b)
      a.to_a.zip(a.coerce(b).to_a).all? { |(x, y)| near?(x, y) }
    end

near?(x, y) click to toggle source

Returns `true` if the two values provided are near each other.

    # File lib/color.rb, line 77
    def near?(x, y)
      (x - y).abs <= Color::COLOR_TOLERANCE
    end

near_one?(value) click to toggle source

Returns `true` if the value is within COLOR_EPSILON of one.

    # File lib/color.rb, line 66
    def near_one?(value)
      near_zero?(value - 1.0)
    end

near_one_or_more?(value) click to toggle source

Returns `true` if the value is within COLOR_EPSILON of one or more than one.

    # File lib/color.rb, line 72
    def near_one_or_more?(value)
      (value > 1.0 or near_one?(value))
    end

near_zero?(value) click to toggle source

Returns `true` if the value is less than COLOR_EPSILON.

    # File lib/color.rb, line 55
    def near_zero?(value)
      (value.abs <= Color::COLOR_EPSILON)
    end

near_zero_or_less?(value) click to toggle source

Returns `true` if the value is within COLOR_EPSILON of zero or less than zero.

    # File lib/color.rb, line 61
    def near_zero_or_less?(value)
      (value < 0.0 or near_zero?(value))
    end

new(values, mode = :rgb) click to toggle source

Provides a thin veneer over the Color module to make it seem like this is Color 0.1.0 (a class) and not Color 1.4 (a module). This "constructor" will be removed in the future.

mode = :hsl | `values` must be an array of [ hue deg, sat %, lum % ]. A Color::HSL object will be created.
mode = :rgb | `values` will either be an HTML-style colour string or an array of [ red, green, blue ] (range 0 .. 255). A Color::RGB object will be created.
mode = :cmyk | `values` must be an array of [ cyan %, magenta %, yellow %, black % ]. A Color::CMYK object will be created.

    # File lib/color.rb, line 167
    def new(values, mode = :rgb)
      warn "Color.new has been deprecated. Use Color::#{mode.to_s.upcase}.new instead."
      color = case mode
              when :hsl
                Color::HSL.new(*values)
              when :rgb
                values = [ values ].flatten
                if values.size == 1
                  Color::RGB.from_html(*values)
                else
                  Color::RGB.new(*values)
                end
              when :cmyk
                Color::CMYK.new(*values)
              end
      color.to_hsl
    end

normalize(value) click to toggle source

Normalizes the value to the range (0.0) .. (1.0).

    # File lib/color.rb, line 96
    def normalize(value)
      if near_zero_or_less? value
        0.0
      elsif near_one_or_more? value
        1.0
      else
        value
      end
    end

Also aliased as: normalize_fractional
normalize_16bit(value) click to toggle source
Alias for: normalize_word
normalize_8bit(value) click to toggle source
Alias for: normalize_byte
normalize_byte(value) click to toggle source

Normalize the value to the range (0) .. (255).

    # File lib/color.rb, line 121
    def normalize_byte(value)
      normalize_to_range(value, 0..255).to_i
    end

Also aliased as: normalize_8bit
normalize_fractional(value) click to toggle source
Alias for: normalize
normalize_to_range(value, range) click to toggle source

Normalizes the value to the specified range.

    # File lib/color.rb, line 108
    def normalize_to_range(value, range)
      range = (range.end..range.begin) if (range.end < range.begin)

      if value <= range.begin
        range.begin
      elsif value >= range.end
        range.end
      else
        value
      end
    end

normalize_word(value) click to toggle source

Normalize the value to the range (0) .. (65535).

    # File lib/color.rb, line 127
    def normalize_word(value)
      normalize_to_range(value, 0..65535).to_i
    end

Also aliased as: normalize_16bit

### Public Instance Methods

==(other) click to toggle source

Compares the `other` colour to this one. The `other` colour will be coerced to the same type as the current colour. Such converted colour comparisons will always be more approximate than non-converted comparisons.

If the `other` colour cannot be coerced to the current colour class, a `NoMethodError` exception will be raised.

All values are compared as floating-point values, so two colours will be reported equivalent if all component values are within COLOR_TOLERANCE of each other.

    # File lib/color.rb, line 33
    def ==(other)
      Color.equivalent?(self, other)
    end

name() click to toggle source

The primary name for the colour.

    # File lib/color.rb, line 38
    def name
      names.first
    end

names() click to toggle source

All names for the colour.

    # File lib/color.rb, line 43
    def names
      self.names = nil unless defined? @names
      @names
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.