File tree Expand file tree Collapse file tree 5 files changed +97
-0
lines changed Expand file tree Collapse file tree 5 files changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module Ass
4
+ # Represents a SubStationAlpha Color
5
+ class Color
6
+ {
7
+ black : [ 0 , 0 , 0 ] ,
8
+ white : [ 255 , 255 , 255 ] ,
9
+ red : [ 255 , 0 , 0 ] ,
10
+ green : [ 0 , 255 , 0 ] ,
11
+ yellow : [ 0 , 255 , 255 ] ,
12
+ } . each do |k , v |
13
+ define_singleton_method ( k ) { new ( *v ) }
14
+ end
15
+
16
+ # @param red [Integer] 0 to 255
17
+ # @param green [Integer] 0 to 255
18
+ # @param blue [Integer] 0 to 255
19
+ def initialize ( red , green , blue )
20
+ @rgb = [ red , green , blue ]
21
+ end
22
+
23
+ # @return [String] ASS script representation
24
+ def to_script
25
+ s = @rgb . map ( &method ( :to_hex ) ) . join ( "" )
26
+ "&H#{ s } &"
27
+ end
28
+
29
+ private
30
+
31
+ def to_hex ( component )
32
+ component . to_s ( 16 ) . upcase . rjust ( 2 , "0" )
33
+ end
34
+ end
35
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module Ass
4
+ # Represents a SubStationAlpha styled String
5
+ class Text
6
+ def initialize ( text )
7
+ @text = text
8
+ @style = { }
9
+ end
10
+
11
+ # @param size [Integer]
12
+ # @return [Text]
13
+ def font_size ( size )
14
+ @style . merge! ( fs : size )
15
+ self
16
+ end
17
+
18
+ # @param size [Integer]
19
+ # @param color [Color]
20
+ # @return [Text]
21
+ def border ( size , color )
22
+ @style . merge! ( bord : size , "3c" : color . to_script )
23
+ self
24
+ end
25
+
26
+ # @param color [Color]
27
+ # @return [Text]
28
+ def color ( color )
29
+ @style . merge! ( "1c" : color . to_script )
30
+ self
31
+ end
32
+
33
+ # @return [String] ASS script representation
34
+ def to_script
35
+ [ @style . map { |k , v | "{\\ #{ k } #{ v } }" } . join , @text ] . join
36
+ end
37
+ end
38
+ end
Original file line number Diff line number Diff line change 5
5
module MPV
6
6
end
7
7
8
+ require_relative "ass/color"
9
+ require_relative "ass/text"
10
+
8
11
require_relative "mpv/exceptions"
9
12
require_relative "mpv/utils"
10
13
require_relative "mpv/client"
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Ass ::Color do
6
+ it "converts white" do
7
+ expect ( Ass ::Color . white . to_script ) . to eql ( "&HFFFFFF&" )
8
+ end
9
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Ass ::Text do
6
+ it "deals with styled text" do
7
+ colors = Ass ::Color
8
+ s = Ass ::Text . new ( "適当" )
9
+ s = s . border ( 1 , colors . black ) . color ( colors . white ) . font_size ( 40 )
10
+ expect ( s . to_script ) . to eql ( "{\\ bord1}{\\ 3c&H000000&}{\\ 1c&HFFFFFF&}{\\ fs40}適当" )
11
+ end
12
+ end
You can’t perform that action at this time.
0 commit comments