Class: Ass::Color

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

Overview

Represents an ASS color.

Constant Summary collapse

PRESETS =
{
  # grayscale
  black:     [  0,   0,   0],
  carbon:    [ 64,  64,  64],
  gray:      [128, 128, 128],
  silver:    [192, 192, 192],
  white:     [255, 255, 255],

  # primary
  red:       [255,   0,   0],
  green:     [  0, 255,   0],
  blue:      [  0,   0, 255],

  # secondary
  yellow:    [255, 255,   0],
  magenta:   [255,   0, 255],
  cyan:      [  0, 255, 255],

  # vivid
  orange:    [255, 128,   0],
  gold:      [255, 192,   0],
  purple:    [128,   0, 255],
  turquoise: [  0, 255, 128],

  # pastel
  pink:      [255, 128, 255],
  sky:       [128, 255, 255],
  mint:      [128, 255, 128],
  vanilla:   [255, 255, 128],
  coral:     [255, 128, 128],

  # shade
  maroon:    [128,   0,   0],
  forest:    [  0, 128,   0],
  navy:      [  0,   0, 128],
  teal:      [  0, 128, 128],
  plum:      [128,   0, 128],
  olive:     [128, 128,   0],
  brown:     [128,  64,   0],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ Color

Creates an ASS::Color.

Parameters:

  • color (Array<Integer>, Symbol)

    preset symbol or custom color codes Color codes are given as [red, green, blue, alpha] in the range 0..255. Alpha defaults to 0, meaning fully opaque, while 255 means fully transparent.



56
57
58
# File 'lib/mpv_ass/color.rb', line 56

def initialize(color)
  set(color)
end

Instance Attribute Details

#codesArray<Integer> (readonly) Also known as: to_a

Gets the RGBA component values.

Returns:

  • (Array<Integer>)

    red, green, blue and alpha codes



49
50
51
# File 'lib/mpv_ass/color.rb', line 49

def codes
  @codes
end

Instance Method Details

#alpha(code) ⇒ self

Changes the alpha value.

Parameters:

  • code (Integer)

    alpha channel value in the range 0..255 0 means fully opaque, while 255 means fully transparent

Returns:

  • (self)


99
100
101
102
# File 'lib/mpv_ass/color.rb', line 99

def alpha(code)
  @codes[3] = code.to_i.clamp(0, 255)
  self
end

#blue(code) ⇒ self

Changes the blue value.

Parameters:

  • code (Integer)

    blue channel value in the range 0..255

Returns:

  • (self)


90
91
92
93
# File 'lib/mpv_ass/color.rb', line 90

def blue(code)
  @codes[2] = code.to_i.clamp(0, 255)
  self
end

#green(code) ⇒ self

Changes the green value.

Parameters:

  • code (Integer)

    green channel value in the range 0..255

Returns:

  • (self)


82
83
84
85
# File 'lib/mpv_ass/color.rb', line 82

def green(code)
  @codes[1] = code.to_i.clamp(0, 255)
  self
end

#red(code) ⇒ self

Changes the red value.

Parameters:

  • code (Integer)

    red channel value in the range 0..255

Returns:

  • (self)


74
75
76
77
# File 'lib/mpv_ass/color.rb', line 74

def red(code)
  @codes[0] = code.to_i.clamp(0, 255)
  self
end

#set(color) ⇒ self

Changes the color value.

Parameters:

  • color (Array<Integer>, Symbol)

    preset symbol or custom color codes Color codes are given as [red, green, blue, alpha] in the range 0..255. Alpha defaults to 0, meaning fully opaque, while 255 means fully transparent.

Returns:

  • (self)


65
66
67
68
69
# File 'lib/mpv_ass/color.rb', line 65

def set(color)
  color = PRESETS.fetch(color) if color.is_a?(Symbol)
  @codes = color.to_a.values_at(0..3).map{ |code| code.to_i.clamp(0, 255) }
  self
end

#to_scriptString

Converts the color to ASS syntax.

Returns:

  • (String)

    ASS color representation



106
107
108
109
# File 'lib/mpv_ass/color.rb', line 106

def to_script
  codes = @codes.last.zero? ? @codes[0..-2] : @codes
  "&H#{codes.reverse.map{ |code| "%02X" % code }.join}&"
end