Class: RubyCord::Color
- Inherits:
-
Object
- Object
- RubyCord::Color
- Defined in:
- lib/rubycord/color.rb
Overview
Represents RGB color.
Instance Attribute Summary collapse
-
#value ⇒ Integer
The color value.
Class Method Summary collapse
-
.[](color) ⇒ RubyCord::Color
Create a color from a Discord's color.
-
.from_hex(hex) ⇒ RubyCord::Color
Create a color from a hexadecimal string.
-
.from_rgb(red, green, blue) ⇒ RubyCord::Color
Create a color from a RGB array.
Instance Method Summary collapse
-
#initialize(value) ⇒ Color
constructor
Create a color from a Integer.
- #inspect ⇒ Object
-
#to_hex ⇒ String
Convert a color to a hexadecimal value.
-
#to_i ⇒ Integer
Integerize a color.
-
#to_rgb ⇒ Array(Integer, Integer, Integer)
(also: #to_a, #deconstruct)
Convert a color to RGB array.
-
#to_rgb_hash ⇒ Hash{:r, :g, :b => Integer}
(also: #deconstruct_keys)
Convert a color to RGB hash.
-
#to_s ⇒ String
Converts a color to a
#000000
string.
Constructor Details
#initialize(value) ⇒ Color
Create a color from a Integer.
64 65 66 |
# File 'lib/rubycord/color.rb', line 64 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Integer
Returns The color value.
24 25 26 |
# File 'lib/rubycord/color.rb', line 24 def value @value end |
Class Method Details
.[](color) ⇒ RubyCord::Color
Create a color from a Discord's color. Currently these colors are supported:
Color name | Color code |
---|---|
:teal |
#1abc9c |
:dark_teal |
#11806a |
:green |
#2ecc71 |
:dark_green |
#1f8b4c |
:blue |
#3498db |
:dark_blue |
#206694 |
:purple |
#9b59b6 |
:dark_purple |
#71368a |
:magenta |
#e91e63 |
:dark_magenta |
#ad1457 |
:gold |
#f1c40f |
:dark_gold |
#c27c0e |
:orange |
#e67e22 |
:dark_orange |
#a84300 |
:red |
#e74c3c |
:dark_red |
#992d22 |
:lighter_grey |
#95a5a6 |
:lighter_gray |
#95a5a6 |
:dark_grey |
#607d8b |
:dark_gray |
#607d8b |
:light_grey |
#979c9f |
:light_gray |
#979c9f |
:darker_grey |
#546e7a |
:darker_gray |
#546e7a |
:og_blurple |
#7289da |
:blurple |
#5865f2 |
:greyple |
#99aab5 |
:dark_theme |
#36393f |
:fuchsia |
#eb459e |
:yellow |
#fee75c |
187 188 189 |
# File 'lib/rubycord/color.rb', line 187 def self.[](color) new(@discord_colors[color]) end |
.from_hex(hex) ⇒ RubyCord::Color
Create a color from a hexadecimal string.
129 130 131 |
# File 'lib/rubycord/color.rb', line 129 def self.from_hex(hex) new(hex.to_i(16)) end |
.from_rgb(red, green, blue) ⇒ RubyCord::Color
Create a color from a RGB array.
142 143 144 |
# File 'lib/rubycord/color.rb', line 142 def self.from_rgb(red, green, blue) new((red * 256 * 256) + (green * 256) + blue) end |
Instance Method Details
#inspect ⇒ Object
118 119 120 |
# File 'lib/rubycord/color.rb', line 118 def inspect "#<#{self.class} #{@value}/#{self}>" end |
#to_hex ⇒ String
Convert a color to a hexadecimal value.
82 83 84 |
# File 'lib/rubycord/color.rb', line 82 def to_hex @value.to_s(16).rjust(6, "0") end |
#to_i ⇒ Integer
Integerize a color.
73 74 75 |
# File 'lib/rubycord/color.rb', line 73 def to_i @value end |
#to_rgb ⇒ Array(Integer, Integer, Integer) Also known as: to_a, deconstruct
Convert a color to RGB array.
91 92 93 |
# File 'lib/rubycord/color.rb', line 91 def to_rgb [@value / (256 * 256), @value / 256 % 256, @value % 256] end |
#to_rgb_hash ⇒ Hash{:r, :g, :b => Integer} Also known as: deconstruct_keys
Convert a color to RGB hash.
103 104 105 |
# File 'lib/rubycord/color.rb', line 103 def to_rgb_hash { r: @value / (256 * 256), g: @value / 256 % 256, b: @value % 256 } end |
#to_s ⇒ String
Converts a color to a #000000
string.
114 115 116 |
# File 'lib/rubycord/color.rb', line 114 def to_s "##{to_hex}" end |