Class: Object::GreyScale

### Home Classes Methods

### In Files

- lib/color.rb
- lib/color/grayscale.rb

### Parent

Object

### Methods

- ::from_fraction
- ::from_percent
- ::new
- #+
- #-
- #-@
- #brightness
- #coerce
- #css_hsl
- #css_hsla
- #css_rgb
- #css_rgba
- #darken_by
- #g
- #g=
- #gray
- #gray=
- #grey
- #grey=
- #html
- #inspect
- #lighten_by
- #pdf_fill
- #pdf_stroke
- #to_a
- #to_cmyk
- #to_grayscale
- #to_greyscale
- #to_hsl
- #to_rgb
- #to_yiq

### Included Modules

- Color

### 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.

# Object::GreyScale

A colour object representing shades of grey. Used primarily in PDF document creation.

### Constants

PDF_FORMAT_STR

  The format of a DeviceGrey colour for PDF. In color-tools 2.0 this will be removed from this package and added back as a modification by the PDF::Writer package.

### Public Class Methods

from_fraction(g = 0, &block) click to toggle source

Creates a greyscale colour object from fractional values 0..1.

    Color::GreyScale.from_fraction(0.5)

    # File lib/color/grayscale.rb, line 15
    def from_fraction(g = 0, &block)
      new(g, 1.0, &block)
    end

from_percent(g = 0, &block) click to toggle source

Creates a greyscale colour object from percentages 0..100.

    Color::GrayScale.from_percent(50)

    # File lib/color/grayscale.rb, line 22
    def from_percent(g = 0, &block)
      new(g, &block)
    end

new(g = 0, radix = 100.0, &block) click to toggle source

Creates a greyscale colour object from percentages 0..100.

    Color::GrayScale.new(50)

    # File lib/color/grayscale.rb, line 30
    def initialize(g = 0, radix = 100.0, &block) # :yields self:
      @g = Color.normalize(g / radix)
      block.call if block
    end

### Public Instance Methods

+(other) click to toggle source

Adds another colour to the current colour. The other colour will be converted to grayscale before addition. This conversion depends upon a to_grayscale method on the other colour.

The addition is done using the grayscale accessor methods to ensure a valid colour in the result.

    # File lib/color/grayscale.rb, line 167
    def +(other)
      self.class.from_fraction(g + other.to_grayscale.g)
    end

-(other) click to toggle source

Subtracts another colour to the current colour. The other colour will be converted to grayscale before subtraction. This conversion depends upon a to_grayscale method on the other colour.

The subtraction is done using the grayscale accessor methods to ensure a valid colour in the result.

    # File lib/color/grayscale.rb, line 177
    def -(other)
      self + (-other)
    end

-@() click to toggle source

    # File lib/color/grayscale.rb, line 189
    def -@
      gs = self.dup
      gs.instance_variable_set(:@g, -g)
      gs
    end

brightness() click to toggle source

Returns the brightness value for this greyscale value; this is the greyscale value itself.

    # File lib/color/grayscale.rb, line 135
    def brightness
      @g
    end

coerce(other) click to toggle source

Coerces the other Color object to grayscale.

    # File lib/color/grayscale.rb, line 36
    def coerce(other)
      other.to_grayscale
    end

css_hsl() click to toggle source

Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180, 25%, 35%)"). Note that this will perform a to_hsl operation.

    # File lib/color/grayscale.rb, line 77
    def css_hsl
      to_hsl.css_hsl
    end

css_hsla() click to toggle source

Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g., "hsla(180, 25%, 35%, 1)"). Note that this will perform a to_hsl operation.

    # File lib/color/grayscale.rb, line 84
    def css_hsla
      to_hsl.css_hsla
    end

css_rgb() click to toggle source

Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%, 100%)").

    # File lib/color/grayscale.rb, line 65
    def css_rgb
      "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ gray, gray, gray ]
    end

css_rgba() click to toggle source

Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g., "rgb(0%, 50%, 100%, 1)").

    # File lib/color/grayscale.rb, line 71
    def css_rgba
      "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %1.2f)" % [ gray, gray, gray, 1 ]
    end

darken_by(percent) click to toggle source

Darken the greyscale colour by the stated percent.

    # File lib/color/grayscale.rb, line 112
    def darken_by(percent)
      g = [@g - (@g * (percent / 100.0)), 0.0].max
      Color::GrayScale.from_fraction(g)
    end

g() click to toggle source

Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.

    # File lib/color/grayscale.rb, line 147
    def g
      @g
    end

g=(gg) click to toggle source

Returns the grayscale value as a fractional value of white in the range 0.0 .. 1.0.

    # File lib/color/grayscale.rb, line 157
    def g=(gg)
      @g = Color.normalize(gg)
    end

gray() click to toggle source

Returns the grayscale value as a percentage of white (100% gray is white).

    # File lib/color/grayscale.rb, line 141
    def gray
      @g * 100.0
    end

Also aliased as: grey
gray=(gg) click to toggle source

Sets the grayscale value as a percentage of white.

    # File lib/color/grayscale.rb, line 151
    def gray=(gg)
      @g = Color.normalize(gg / 100.0)
    end

Also aliased as: grey=
grey() click to toggle source
Alias for: gray
grey=(gg) click to toggle source
Alias for: gray=
html() click to toggle source

Present the colour as an HTML/CSS colour string.

    # File lib/color/grayscale.rb, line 58
    def html
      gs = "%02x" % to_255
      "##{gs * 3}"
    end

inspect() click to toggle source

    # File lib/color/grayscale.rb, line 181
    def inspect
      "Gray [%.2f%%]" % [ gray ]
    end

lighten_by(percent) click to toggle source

Lightens the greyscale colour by the stated percent.

    # File lib/color/grayscale.rb, line 106
    def lighten_by(percent)
      g = [@g + (@g * (percent / 100.0)), 1.0].min
      Color::GrayScale.from_fraction(g)
    end

pdf_fill() click to toggle source

Present the colour as a DeviceGrey fill colour string for PDF. This will be removed from the default package in color-tools 2.0.

    # File lib/color/grayscale.rb, line 42
    def pdf_fill
      PDF_FORMAT_STR % [ @g, "g" ]
    end

pdf_stroke() click to toggle source

Present the colour as a DeviceGrey stroke colour string for PDF. This will be removed from the default package in color-tools 2.0.

    # File lib/color/grayscale.rb, line 48
    def pdf_stroke
      PDF_FORMAT_STR % [ @g, "G" ]
    end

to_a() click to toggle source

    # File lib/color/grayscale.rb, line 185
    def to_a
      [ g ]
    end

to_cmyk() click to toggle source

Convert the greyscale colour to CMYK.

    # File lib/color/grayscale.rb, line 89
    def to_cmyk
      k = 1.0 - @g.to_f
      Color::CMYK.from_fraction(0, 0, 0, k)
    end

to_grayscale() click to toggle source

Reflexive conversion.

    # File lib/color/grayscale.rb, line 100
    def to_grayscale
      self
    end

Also aliased as: to_greyscale
to_greyscale() click to toggle source
Alias for: to_grayscale
to_hsl() click to toggle source

Returns the HSL colour encoding of the greyscale value.

    # File lib/color/grayscale.rb, line 129
    def to_hsl
      Color::HSL.from_fraction(0, 0, @g)
    end

to_rgb(ignored = true) click to toggle source

Convert the greyscale colour to RGB.

    # File lib/color/grayscale.rb, line 95
    def to_rgb(ignored = true)
      Color::RGB.from_fraction(g, g, g)
    end

to_yiq() click to toggle source

Returns the YIQ (NTSC) colour encoding of the greyscale value. This is an approximation, as the values for I and Q are calculated by treating the greyscale value as an RGB value. The Y (intensity or brightness) value is the same as the greyscale value.

    # File lib/color/grayscale.rb, line 121
    def to_yiq
      y = @g
      i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
      q = (@g * 0.212) + (@g * -0.523) + (@g *  0.311)
      Color::YIQ.from_fraction(y, i, q)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.