Class: DiscordRDA::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/entity/color.rb

Overview

Represents a Discord color. Provides conversion between RGB, hex, and integer representations.

Examples:

Creating colors

Color.new(0xFF5733)           # From integer
Color.from_rgb(255, 87, 51)    # From RGB
Color.from_hex('#FF5733')      # From hex string
Color.teal                     # Named color

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0) ⇒ Color

Initialize with a color value

Parameters:

  • value (Integer) (defaults to: 0)

    Color value (0-0xFFFFFF)



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

#valueInteger (readonly)

Returns The color value.

Returns:

  • (Integer)

    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

Parameters:

  • hex (String)

    Hex string (#RRGGBB or RRGGBB)

Returns:



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

Parameters:

  • h (Float)

    Hue (0-360)

  • s (Float)

    Saturation (0-1)

  • l (Float)

    Lightness (0-1)

Returns:



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

Parameters:

  • r (Integer)

    Red (0-255)

  • g (Integer)

    Green (0-255)

  • b (Integer)

    Blue (0-255)

Returns:



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

.randomColor

Generate random color

Returns:

  • (Color)

    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

Parameters:

  • other (Object)

    Other object

Returns:

  • (Boolean)

    True if equal



237
238
239
# File 'lib/discord_rda/entity/color.rb', line 237

def ==(other)
  other.is_a?(Color) && @value == other.value
end

#bInteger Also known as: blue

Get blue component

Returns:

  • (Integer)

    Blue (0-255)



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.

Returns:

  • (Color)

    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

#brightnessInteger

Get brightness (0-255)

Returns:

  • (Integer)

    Brightness



187
188
189
# File 'lib/discord_rda/entity/color.rb', line 187

def brightness
  (r * 299 + g * 587 + b * 114) / 1000
end

#complementaryColor

Get complementary color

Returns:

  • (Color)

    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)

Returns:

  • (Boolean)

    True if dark



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

Parameters:

  • amount (Float) (defaults to: 0.2)

    Amount to darken (0-1)

Returns:

  • (Color)

    Darkened 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)

Returns:

  • (Boolean)

    True if default



181
182
183
# File 'lib/discord_rda/entity/color.rb', line 181

def default?
  @value == 0
end

#gInteger Also known as: green

Get green component

Returns:

  • (Integer)

    Green (0-255)



129
130
131
# File 'lib/discord_rda/entity/color.rb', line 129

def g
  (@value >> 8) & 0xFF
end

#hashInteger

Hash code

Returns:

  • (Integer)

    Hash code



243
244
245
# File 'lib/discord_rda/entity/color.rb', line 243

def hash
  @value.hash
end

#inspectString

Inspect

Returns:

  • (String)

    Inspect string



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)

Returns:

  • (Boolean)

    True if light



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

Parameters:

  • amount (Float) (defaults to: 0.2)

    Amount to lighten (0-1)

Returns:

  • (Color)

    Lightened 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

#rInteger Also known as: red

Get red component

Returns:

  • (Integer)

    Red (0-255)



122
123
124
# File 'lib/discord_rda/entity/color.rb', line 122

def r
  (@value >> 16) & 0xFF
end

#rgbArray<Integer>

Get RGB array

Returns:

  • (Array<Integer>)

    RGB values



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

Parameters:

  • prefix (Boolean) (defaults to: true)

    Include # prefix

Returns:

  • (String)

    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_iInteger

Get integer value

Returns:

  • (Integer)

    Color value



163
164
165
# File 'lib/discord_rda/entity/color.rb', line 163

def to_i
  @value
end

#to_rgb_stringString

Convert to RGB tuple string

Returns:

  • (String)

    RGB string



157
158
159
# File 'lib/discord_rda/entity/color.rb', line 157

def to_rgb_string
  "rgb(#{r}, #{g}, #{b})"
end

#to_sString

Convert to decimal color string (for Discord)

Returns:

  • (String)

    Decimal string



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)

Returns:

  • (Boolean)

    True if has color



175
176
177
# File 'lib/discord_rda/entity/color.rb', line 175

def valid?
  @value > 0
end