Class: Discorb::Color
- Inherits:
-
Object
- Object
- Discorb::Color
- Defined in:
- lib/discorb/color.rb
Overview
Represents RGB color.
Instance Attribute Summary collapse
-
#value -> Object
Returns the value of attribute value.
Class Method Summary collapse
-
.[](color) -> Discorb::Color
Create a color from a Discord's color.
-
.from_hex(hex) -> Discorb::Color
Create a color from a hexadecimal string.
-
.from_rgb(red, green, blue) -> Discorb::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.
48 49 50 |
# File 'lib/discorb/color.rb', line 48 def initialize(value) @value = value end |
Instance Attribute Details
#value -> Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/discorb/color.rb', line 8 def value @value end |
Class Method Details
.[](color) -> Discorb::Color
Create a color from a Discord's color. Currently these colors are supported: - teal (0x1abc9c) - dark_teal (0x11806a) - green (0x2ecc71) - dark_green (0x1f8b4c) - blue (0x3498db) - dark_blue (0x206694) - purple (0x9b59b6) - dark_purple (0x71368a) - magenta (0xe91e63) - dark_magenta (0xad1457) - gold (0xf1c40f) - dark_gold (0xc27c0e) - orange (0xe67e22) - dark_orange (0xa84300) - red (0xe74c3c) - dark_red (0x992d22) - lighter_grey (0x95a5a6) - lighter_gray (0x95a5a6) - dark_grey (0x607d8b) - dark_gray (0x607d8b) - light_grey (0x979c9f) - light_gray (0x979c9f) - darker_grey (0x546e7a) - darker_gray (0x546e7a) - og_blurple (0x7289da) - blurple (0x5865f2) - greyple (0x99aab5) - dark_theme (0x36393f) - fuchsia (0xeb459e)
167 168 169 |
# File 'lib/discorb/color.rb', line 167 def self.[](color) new(@discord_colors[color]) end |
.from_hex(hex) -> Discorb::Color
Create a color from a hexadecimal string.
113 114 115 |
# File 'lib/discorb/color.rb', line 113 def self.from_hex(hex) new(hex.to_i(16)) end |
.from_rgb(red, green, blue) -> Discorb::Color
Create a color from a RGB array.
126 127 128 |
# File 'lib/discorb/color.rb', line 126 def self.from_rgb(red, green, blue) new(red * 256 * 256 + green * 256 + blue) end |
Instance Method Details
#inspect -> Object
102 103 104 |
# File 'lib/discorb/color.rb', line 102 def inspect "#<#{self.class} #{@value}/#{self}>" end |
#to_hex -> String
Convert a color to a hexadecimal value.
66 67 68 |
# File 'lib/discorb/color.rb', line 66 def to_hex @value.to_s(16).rjust(6, "0") end |
#to_i -> Integer
Integerize a color.
57 58 59 |
# File 'lib/discorb/color.rb', line 57 def to_i @value end |
#to_rgb -> Array(Integer, Integer, Integer) Also known as: to_a, deconstruct
Convert a color to RGB array.
75 76 77 |
# File 'lib/discorb/color.rb', line 75 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.
87 88 89 |
# File 'lib/discorb/color.rb', line 87 def to_rgb_hash [@value / (256 * 256), @value / 256 % 256, @value % 256] end |
#to_s -> String
Converts a color to a #000000
string.
98 99 100 |
# File 'lib/discorb/color.rb', line 98 def to_s "##{to_hex}" end |