Class: Color::Grayscale
- Inherits:
-
Data
- Object
- Data
- Color::Grayscale
- 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
Instance Attribute Summary collapse
-
#g ⇒ Object
(also: #brightness)
readonly
Returns the value of attribute g.
Class Method Summary collapse
-
.from_percentage(*args, **kwargs) ⇒ Object
(also: from_values)
Creates a grayscale color object from a percentage value (0.0 .. 100.0).
Instance Method Summary collapse
-
#coerce(other) ⇒ Object
Coerces the other Color object to grayscale.
-
#components ⇒ Object
:nodoc:.
-
#css(alpha: nil) ⇒ Object
Present the color as a CSS ‘rgb` color with optional `alpha`.
-
#darken_by(percent) ⇒ Object
Darken the grayscale color by the stated percent.
- #deconstruct_keys(_keys) ⇒ Object
- #gray ⇒ Object
-
#html ⇒ Object
Present the color as an HTML/CSS color string (e.g., ‘#dddddd`).
-
#initialize(g:) ⇒ Grayscale
constructor
Creates a grayscale color object from a fractional value (0.0 .. 1.0).
-
#inspect ⇒ Object
:nodoc:.
-
#lighten_by(percent) ⇒ Object
Lightens the grayscale color by the stated percent.
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#to_a ⇒ Object
(also: #deconstruct)
:nodoc:.
-
#to_cmyk ⇒ Object
Convert Grayscale to Color::CMYK.
- #to_grayscale ⇒ Object
-
#to_hsl ⇒ Object
Converts Grayscale to Color::HSL.
-
#to_internal ⇒ Object
:nodoc:.
-
#to_lab ⇒ Object
Converts Grayscale to Color::CIELAB via Color::RGB.
-
#to_rgb ⇒ Object
Convert Grayscale to Color::RGB.
-
#to_xyz ⇒ Object
Converts Grayscale to Color::XYZ via Color::RGB.
-
#to_yiq ⇒ Object
Convert Grayscale to Color::YIQ.
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
#g ⇒ Object (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 |
#components ⇒ Object
:nodoc:
168 |
# File 'lib/color/grayscale.rb', line 168 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) “`
122 123 124 125 126 127 |
# File 'lib/color/grayscale.rb', line 122 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.
135 |
# File 'lib/color/grayscale.rb', line 135 def darken_by(percent) = Color::Grayscale.from_fraction([g - (g * (percent / 100.0)), 0.0].max) |
#deconstruct_keys(_keys) ⇒ Object
162 |
# File 'lib/color/grayscale.rb', line 162 def deconstruct_keys(_keys) = {g:, gray:} |
#gray ⇒ Object
141 |
# File 'lib/color/grayscale.rb', line 141 def gray = g * 100.0 |
#html ⇒ Object
Present the color as an HTML/CSS color string (e.g., ‘#dddddd`).
111 112 113 |
# File 'lib/color/grayscale.rb', line 111 def html "##{("%02x" % translate_range(g, to: 0.0..255.0)) * 3}" end |
#inspect ⇒ Object
:nodoc:
144 |
# File 'lib/color/grayscale.rb', line 144 def inspect = "Grayscale [%.2f%%]" % [gray] # :nodoc: |
#lighten_by(percent) ⇒ Object
Lightens the grayscale color by the stated percent.
131 |
# File 'lib/color/grayscale.rb', line 131 def lighten_by(percent) = Color::Grayscale.from_fraction([g + (g * (percent / 100.0)), 1.0].min) |
#pretty_print(q) ⇒ Object
:nodoc:
147 148 149 150 151 152 153 |
# File 'lib/color/grayscale.rb', line 147 def pretty_print(q) # :nodoc: q.text "Grayscale" q.breakable q.group 2, "[", "]" do q.text "%.2f%%" % gray end end |
#to_a ⇒ Object Also known as: deconstruct
:nodoc:
156 |
# File 'lib/color/grayscale.rb', line 156 def to_a = [gray] # :nodoc: |
#to_cmyk ⇒ Object
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_grayscale ⇒ Object
82 |
# File 'lib/color/grayscale.rb', line 82 def to_grayscale(...) = self |
#to_hsl ⇒ Object
Converts Grayscale to Color::HSL.
103 |
# File 'lib/color/grayscale.rb', line 103 def to_hsl(...) = Color::HSL.from_fraction(0, 0, g) |
#to_internal ⇒ Object
:nodoc:
165 |
# File 'lib/color/grayscale.rb', line 165 def to_internal = [g] # :nodoc: |
#to_lab ⇒ Object
Converts Grayscale to Color::CIELAB via Color::RGB.
107 |
# File 'lib/color/grayscale.rb', line 107 def to_lab(...) = to_rgb(...).to_lab(...) |
#to_rgb ⇒ Object
Convert Grayscale to Color::RGB.
79 |
# File 'lib/color/grayscale.rb', line 79 def to_rgb(...) = Color::RGB.from_fraction(g, g, g) |
#to_xyz ⇒ Object
Converts Grayscale to Color::XYZ via Color::RGB.
86 |
# File 'lib/color/grayscale.rb', line 86 def to_xyz(...) = to_rgb(...).to_xyz(...) |
#to_yiq ⇒ Object
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.
94 95 96 97 98 99 |
# File 'lib/color/grayscale.rb', line 94 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 |