Class: Color::YIQ
- Inherits:
-
Data
- Object
- Data
- Color::YIQ
- Includes:
- Color
- Defined in:
- lib/color/yiq.rb,
lib/color.rb
Overview
A Color object representing YIQ (NTSC) color encoding, where Y is the luma (brightness) value, and I (orange-blue) and Q (purple-green) are chrominance.
All values are clamped between 0 and 1 inclusive.
More more details, see [YIQ].
[wikiyiq]: en.wikipedia.org/wiki/YIQ
YIQ colors are immutable Data class instances. Array deconstruction is ‘[y, i, q]` and hash deconstruction is `i:, q:` (see #y, #i, #q).
YIQ is only partially implemented: other Color objects can only be converted to YIQ, but it has few conversion functions for converting from YIQ.
Constant Summary
Constants included from Color
Instance Attribute Summary collapse
-
#i ⇒ Object
readonly
Returns the value of attribute i.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
-
#y ⇒ Object
(also: #brightness)
readonly
Returns the value of attribute y.
Class Method Summary collapse
-
.from_percentage(*args, **kwargs) ⇒ Object
(also: from_values)
Creates a YIQ color object from percentage values 0 ..
Instance Method Summary collapse
-
#coerce(other) ⇒ Object
Coerces the other Color object into YIQ.
-
#deconstruct_keys(_keys) ⇒ Object
:nodoc:.
-
#initialize(y:, i:, q:) ⇒ YIQ
constructor
Creates a YIQ color object from fractional values 0 ..
-
#inspect ⇒ Object
:nodoc:.
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#to_grayscale ⇒ Object
Convert YIQ to Color::Grayscale using the luma (#y) value.
- #to_yiq ⇒ Object
Methods included from Color
#==, #components, #css_value, #map, #map_with, normalize, #scale, translate_range, #zip
Constructor Details
#initialize(y:, i:, q:) ⇒ YIQ
Creates a YIQ color object from fractional values 0 .. 1.
“‘ruby Color::YIQ.from_fraction(0.3, 0.2, 0.1) # => YIQ [30% 20% 10%] Color::YIQ.new(0.3, 0.2, 0.1) # => YIQ [30% 20% 10%] Color::YIQ[y: 0.3, i: 0.2, q: 0.1] # => YIQ [30% 20% 10%] “`
71 72 73 |
# File 'lib/color/yiq.rb', line 71 def initialize(y:, i:, q:) # :nodoc: super(y: normalize(y), i: normalize(i), q: normalize(q)) end |
Instance Attribute Details
#i ⇒ Object (readonly)
Returns the value of attribute i
50 51 52 |
# File 'lib/color.rb', line 50 def i @i end |
#q ⇒ Object (readonly)
Returns the value of attribute q
50 51 52 |
# File 'lib/color.rb', line 50 def q @q end |
#y ⇒ Object (readonly) Also known as: brightness
Returns the value of attribute y
50 51 52 |
# File 'lib/color.rb', line 50 def y @y end |
Class Method Details
.from_percentage(*args, **kwargs) ⇒ Object Also known as: from_values
Creates a YIQ color object from percentage values 0 .. 1.
“‘ruby Color::YIQ.from_percentage(30, 20, 10) # => YIQ [30% 20% 10%] Color::YIQ.from_percentage(y: 30, i: 20, q: 10) # => YIQ [30% 20% 10%] Color::YIQ.from_values(30, 20, 10) # => YIQ [30% 20% 10%] Color::YIQ.from_values(y: 30, i: 20, q: 10) # => YIQ [30% 20% 10%] “`
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/color/yiq.rb', line 43 def self.from_percentage(*args, **kwargs) y, i, q = case [args, kwargs] in [[_, _, _], {}] args in [[], {y:, i:, q:}] [y, i, q] else new(*args, **kwargs) end new(y: y / 100.0, i: i / 100.0, q: q / 100.0) end |
Instance Method Details
#coerce(other) ⇒ Object
Coerces the other Color object into YIQ.
77 |
# File 'lib/color/yiq.rb', line 77 def coerce(other) = other.to_yiq |
#deconstruct_keys(_keys) ⇒ Object
:nodoc:
106 |
# File 'lib/color/yiq.rb', line 106 def deconstruct_keys(_keys) = {y:, i:, q:} # :nodoc: |
#inspect ⇒ Object
:nodoc:
89 |
# File 'lib/color/yiq.rb', line 89 def inspect = "YIQ [%.2f%% %.2f%% %.2f%%]" % [y * 100, i * 100, q * 100] # :nodoc: |
#pretty_print(q) ⇒ Object
:nodoc:
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/color/yiq.rb', line 91 def pretty_print(q) # :nodoc: q.text "YIQ" q.breakable q.group 2, "[", "]" do q.text "%.2f%%" % y q.fill_breakable q.text "%.2f%%" % i q.fill_breakable q.text "%.2f%%" % q end end |
#to_grayscale ⇒ Object
Convert YIQ to Color::Grayscale using the luma (#y) value.
84 |
# File 'lib/color/yiq.rb', line 84 def to_grayscale = Color::Grayscale.from_fraction(y) |
#to_yiq ⇒ Object
80 |
# File 'lib/color/yiq.rb', line 80 def to_yiq = self |