Class: DiscordRDA::Color
- Inherits:
-
Object
- Object
- DiscordRDA::Color
- Defined in:
- lib/discord_rda/entity/color.rb
Overview
Represents a Discord color. Provides conversion between RGB, hex, and integer representations.
Constant Summary collapse
- DEFAULTS =
Default Discord colors
{ default: 0, white: 0xFFFFFF, aqua: 0x1ABC9C, green: 0x57F287, blue: 0x3498DB, yellow: 0xFEE75C, purple: 0x9B59B6, luminous_vivid_pink: 0xE91E63, gold: 0xF1C40F, orange: 0xE67E22, red: 0xED4245, grey: 0x95A5A6, navy: 0x34495E, dark_aqua: 0x11806A, dark_green: 0x1F8B4C, dark_blue: 0x206694, dark_purple: 0x71368A, dark_vivid_pink: 0xAD1457, dark_gold: 0xC27C0E, dark_orange: 0xA84300, dark_red: 0x992D22, dark_grey: 0x979C9F, darker_grey: 0x7F8C8D, light_grey: 0xBCC0C0, dark_navy: 0x2C3E50, blurple: 0x5865F2, greyple: 0x99AAB5, dark_but_not_black: 0x2C2F33, not_quite_black: 0x23272A }.freeze
Instance Attribute Summary collapse
-
#value ⇒ Integer
readonly
The color value.
Class Method Summary collapse
-
.from_hex(hex) ⇒ Color
Create from hex string.
-
.from_hsl(h, s, l) ⇒ Color
Create from HSL values.
-
.from_rgb(r, g, b) ⇒ Color
Create from RGB values.
-
.random ⇒ Color
Generate random color.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality.
-
#b ⇒ Integer
(also: #blue)
Get blue component.
-
#blend(other, ratio = 0.5) ⇒ Color
Blended color.
-
#brightness ⇒ Integer
Get brightness (0-255).
-
#complementary ⇒ Color
Get complementary color.
-
#dark? ⇒ Boolean
Check if color is dark (brightness <= 128).
-
#darken(amount = 0.2) ⇒ Color
Darken the color.
-
#default? ⇒ Boolean
Check if color is the default (0).
-
#g ⇒ Integer
(also: #green)
Get green component.
-
#hash ⇒ Integer
Hash code.
-
#initialize(value = 0) ⇒ Color
constructor
Initialize with a color value.
-
#inspect ⇒ String
Inspect.
-
#light? ⇒ Boolean
Check if color is light (brightness > 128).
-
#lighten(amount = 0.2) ⇒ Color
Lighten the color.
-
#r ⇒ Integer
(also: #red)
Get red component.
-
#rgb ⇒ Array<Integer>
Get RGB array.
-
#to_hex(prefix: true) ⇒ String
Get hex string.
-
#to_i ⇒ Integer
Get integer value.
-
#to_rgb_string ⇒ String
Convert to RGB tuple string.
-
#to_s ⇒ String
Convert to decimal color string (for Discord).
-
#valid? ⇒ Boolean
Check if color is valid (non-zero).
Constructor Details
#initialize(value = 0) ⇒ Color
Initialize with a color value
116 117 118 |
# File 'lib/discord_rda/entity/color.rb', line 116 def initialize(value = 0) @value = value.to_i & 0xFFFFFF end |
Instance Attribute Details
#value ⇒ Integer (readonly)
Returns The color value.
48 49 50 |
# File 'lib/discord_rda/entity/color.rb', line 48 def value @value end |
Class Method Details
.from_hex(hex) ⇒ Color
Create from hex string
63 64 65 66 |
# File 'lib/discord_rda/entity/color.rb', line 63 def from_hex(hex) hex = hex.to_s.delete_prefix('#') new(hex.to_i(16)) end |
.from_hsl(h, s, l) ⇒ Color
Create from HSL values
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/discord_rda/entity/color.rb', line 73 def from_hsl(h, s, l) h = h.to_f / 360.0 s = s.to_f l = l.to_f r, g, b = if s == 0 [l, l, l] else q = l < 0.5 ? l * (1 + s) : l + s - l * s p = 2 * l - q [hue_to_rgb(p, q, h + 1.0 / 3), hue_to_rgb(p, q, h), hue_to_rgb(p, q, h - 1.0 / 3)] end from_rgb((r * 255).round, (g * 255).round, (b * 255).round) end |
.from_rgb(r, g, b) ⇒ Color
Create from RGB values
56 57 58 |
# File 'lib/discord_rda/entity/color.rb', line 56 def from_rgb(r, g, b) new((r.to_i << 16) | (g.to_i << 8) | b.to_i) end |
.random ⇒ Color
Generate random color
93 94 95 |
# File 'lib/discord_rda/entity/color.rb', line 93 def random new(rand(0xFFFFFF)) end |
Instance Method Details
#==(other) ⇒ Boolean
Check equality
237 238 239 |
# File 'lib/discord_rda/entity/color.rb', line 237 def ==(other) other.is_a?(Color) && @value == other.value end |
#b ⇒ Integer Also known as: blue
Get blue component
136 137 138 |
# File 'lib/discord_rda/entity/color.rb', line 136 def b @value & 0xFF end |
#blend(other, ratio = 0.5) ⇒ Color
Returns Blended color.
207 208 209 210 211 212 |
# File 'lib/discord_rda/entity/color.rb', line 207 def blend(other, ratio = 0.5) r = (self.r * (1 - ratio) + other.r * ratio).round g = (self.g * (1 - ratio) + other.g * ratio).round b = (self.b * (1 - ratio) + other.b * ratio).round self.class.from_rgb(r, g, b) end |
#brightness ⇒ Integer
Get brightness (0-255)
187 188 189 |
# File 'lib/discord_rda/entity/color.rb', line 187 def brightness (r * 299 + g * 587 + b * 114) / 1000 end |
#complementary ⇒ Color
Get complementary color
230 231 232 |
# File 'lib/discord_rda/entity/color.rb', line 230 def complementary self.class.from_rgb(255 - r, 255 - g, 255 - b) end |
#dark? ⇒ Boolean
Check if color is dark (brightness <= 128)
199 200 201 |
# File 'lib/discord_rda/entity/color.rb', line 199 def dark? brightness <= 128 end |
#darken(amount = 0.2) ⇒ Color
Darken the color
217 218 219 |
# File 'lib/discord_rda/entity/color.rb', line 217 def darken(amount = 0.2) blend(self.class.new(0), amount) end |
#default? ⇒ Boolean
Check if color is the default (0)
181 182 183 |
# File 'lib/discord_rda/entity/color.rb', line 181 def default? @value == 0 end |
#g ⇒ Integer Also known as: green
Get green component
129 130 131 |
# File 'lib/discord_rda/entity/color.rb', line 129 def g (@value >> 8) & 0xFF end |
#hash ⇒ Integer
Hash code
243 244 245 |
# File 'lib/discord_rda/entity/color.rb', line 243 def hash @value.hash end |
#inspect ⇒ String
Inspect
249 250 251 |
# File 'lib/discord_rda/entity/color.rb', line 249 def inspect "#<Color #{to_hex}>" end |
#light? ⇒ Boolean
Check if color is light (brightness > 128)
193 194 195 |
# File 'lib/discord_rda/entity/color.rb', line 193 def light? brightness > 128 end |
#lighten(amount = 0.2) ⇒ Color
Lighten the color
224 225 226 |
# File 'lib/discord_rda/entity/color.rb', line 224 def lighten(amount = 0.2) blend(self.class.new(0xFFFFFF), amount) end |
#r ⇒ Integer Also known as: red
Get red component
122 123 124 |
# File 'lib/discord_rda/entity/color.rb', line 122 def r (@value >> 16) & 0xFF end |
#rgb ⇒ Array<Integer>
Get RGB array
143 144 145 |
# File 'lib/discord_rda/entity/color.rb', line 143 def rgb [r, g, b] end |
#to_hex(prefix: true) ⇒ String
Get hex string
150 151 152 153 |
# File 'lib/discord_rda/entity/color.rb', line 150 def to_hex(prefix: true) hex = @value.to_s(16).upcase.rjust(6, '0') prefix ? "##{hex}" : hex end |
#to_i ⇒ Integer
Get integer value
163 164 165 |
# File 'lib/discord_rda/entity/color.rb', line 163 def to_i @value end |
#to_rgb_string ⇒ String
Convert to RGB tuple string
157 158 159 |
# File 'lib/discord_rda/entity/color.rb', line 157 def to_rgb_string "rgb(#{r}, #{g}, #{b})" end |
#to_s ⇒ String
Convert to decimal color string (for Discord)
169 170 171 |
# File 'lib/discord_rda/entity/color.rb', line 169 def to_s @value.to_s end |
#valid? ⇒ Boolean
Check if color is valid (non-zero)
175 176 177 |
# File 'lib/discord_rda/entity/color.rb', line 175 def valid? @value > 0 end |