Class: Color::Grayscale

Inherits:
Data
  • Object
show all
Includes:
Color
Defined in:
lib/color/grayscale.rb,
lib/color.rb

Overview

Grayscale is a color object representing shades of gray as a ratio of black to white, where 0% (0.0) gray is black and 100% (1.0) gray is white.

Grayscale colors are immutable Data class instances. Array deconstruction is ‘[gray]` and hash deconstruction is `gray:`. See #g, #gray.

Constant Summary

Constants included from Color

EPSILON, TOLERANCE, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Color

#==, #css_value, #map, #map_with, normalize, #scale, translate_range, #zip

Constructor Details

#initialize(g:) ⇒ Grayscale

Creates a grayscale color object from a fractional value (0.0 .. 1.0).

“‘ruby Color::Grayscale.from_fraction(0.5) Color::Grayscale.new(0.5) Color::Grayscale[g: 0.5] “`



65
66
67
# File 'lib/color/grayscale.rb', line 65

def initialize(g:)
  super(g: normalize(g))
end

Instance Attribute Details

#gObject (readonly) Also known as: brightness

Returns the value of attribute g



46
47
48
# File 'lib/color.rb', line 46

def g
  @g
end

Class Method Details

.from_percentage(*args, **kwargs) ⇒ Object Also known as: from_values

Creates a grayscale color object from a percentage value (0.0 .. 100.0).

“‘ruby Color::Grayscale.from_percentage(50) # => Grayscale [0.50%] Color::Grayscale.from_values(50) # => Grayscale [0.50%] “`

:call-seq:

from_percentage(g)
from_percentage(g:)
from_values(g)
from_values(g:)


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/color/grayscale.rb', line 37

def self.from_percentage(*args, **kwargs)
  g =
    case [args, kwargs]
    in [[g], {}]
      g
    in [[], {g:}]
      g
    else
      new(*args, **kwargs)
    end

  new(g: g / 100.0)
end

Instance Method Details

#coerce(other) ⇒ Object

Coerces the other Color object to grayscale.



71
# File 'lib/color/grayscale.rb', line 71

def coerce(other) = other.to_grayscale

#componentsObject

:nodoc:



164
# File 'lib/color/grayscale.rb', line 164

def components = 1 # :nodoc:

#css(alpha: nil) ⇒ Object

Present the color as a CSS ‘rgb` color with optional `alpha`.

“‘ruby Color::Grayscale.css # => rgb(50.00% 50.00% 50.00%) Color::Grayscale.css(alpha: 0.75) # => rgb(50.00% 50.00% 50.00% / 0.75) “`



118
119
120
121
122
123
# File 'lib/color/grayscale.rb', line 118

def css(alpha: nil)
  params = ([css_value(gray, :percent)] * 3).join(" ")
  params = "#{params} / #{css_value(alpha)}" if alpha

  "rgb(#{params})"
end

#darken_by(percent) ⇒ Object

Darken the grayscale color by the stated percent.



131
# File 'lib/color/grayscale.rb', line 131

def darken_by(percent) = Color::Grayscale.from_fraction([g - (g * (percent / 100.0)), 0.0].max)

#deconstruct_keys(_keys) ⇒ Object



158
# File 'lib/color/grayscale.rb', line 158

def deconstruct_keys(_keys) = {g:, gray:}

#grayObject



137
# File 'lib/color/grayscale.rb', line 137

def gray = g * 100.0

#htmlObject

Present the color as an HTML/CSS color string (e.g., ‘#dddddd`).



107
108
109
# File 'lib/color/grayscale.rb', line 107

def html
  "##{("%02x" % translate_range(g, to: 0.0..255.0)) * 3}"
end

#inspectObject

:nodoc:



140
# File 'lib/color/grayscale.rb', line 140

def inspect = "Grayscale [%.2f%%]" % [gray] # :nodoc:

#lighten_by(percent) ⇒ Object

Lightens the grayscale color by the stated percent.



127
# File 'lib/color/grayscale.rb', line 127

def lighten_by(percent) = Color::Grayscale.from_fraction([g + (g * (percent / 100.0)), 1.0].min)

#pretty_print(q) ⇒ Object

:nodoc:



143
144
145
146
147
148
149
# File 'lib/color/grayscale.rb', line 143

def pretty_print(q) # :nodoc:
  q.text "Grayscale"
  q.breakable
  q.group 2, "[", "]" do
    q.text "%.2f%%" % gray
  end
end

#to_aObject Also known as: deconstruct

:nodoc:



152
# File 'lib/color/grayscale.rb', line 152

def to_a = [gray] # :nodoc:

#to_cmykObject

Convert Grayscale to Color::CMYK.



75
# File 'lib/color/grayscale.rb', line 75

def to_cmyk(...) = Color::CMYK.from_fraction(0, 0, 0, 1.0 - g.to_f)

#to_grayscaleObject



82
# File 'lib/color/grayscale.rb', line 82

def to_grayscale(...) = self

#to_hslObject

Converts Grayscale to Color::HSL.



99
# File 'lib/color/grayscale.rb', line 99

def to_hsl(...) = Color::HSL.from_fraction(0, 0, g)

#to_internalObject

:nodoc:



161
# File 'lib/color/grayscale.rb', line 161

def to_internal = [g] # :nodoc:

#to_labObject

Converts Grayscale to Color::CIELAB via Color::RGB.



103
# File 'lib/color/grayscale.rb', line 103

def to_lab(...) = to_rgb(...).to_lab(...)

#to_rgbObject

Convert Grayscale to Color::RGB.



79
# File 'lib/color/grayscale.rb', line 79

def to_rgb(...) = Color::RGB.from_fraction(g, g, g)

#to_yiqObject

Convert Grayscale to Color::YIQ.

This approximates the actual value, as I and Q are calculated by treating the grayscale value as a RGB value. The Y (intensity or brightness) value is the same as the grayscale value.



90
91
92
93
94
95
# File 'lib/color/grayscale.rb', line 90

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