Skip to content

Commit

Permalink
webvtt: add colored output (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlecorfec committed Jan 4, 2021
1 parent 21f9730 commit 9df47df
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion webvtt.go
Expand Up @@ -367,7 +367,16 @@ func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {

func (li LineItem) webVTTBytes() []byte {
var c []byte
if li.InlineStyle != nil && li.InlineStyle.WebVTTItalics {
var color string

if li.InlineStyle != nil && li.InlineStyle.TTMLColor != "" {
color = cssColor(li.InlineStyle.TTMLColor)
}
if color != "" {
c = append(c, []byte("<c."+color+">")...)
c = append(c, []byte(li.Text)...)
c = append(c, []byte("</c>")...)
} else if li.InlineStyle != nil && li.InlineStyle.WebVTTItalics {
c = append(c, []byte("<i>")...)
c = append(c, []byte(li.Text)...)
c = append(c, []byte("</i>")...)
Expand All @@ -376,3 +385,14 @@ func (li LineItem) webVTTBytes() []byte {
}
return c
}

func cssColor(rgb string) string {
colors := map[string]string{
"#00ffff": "cyan", // narrator, thought
"#ffff00": "yellow", // out of vision
"#ff0000": "red", // noises
"#ff00ff": "magenta", // song
"#00ff00": "lime", // foreign speak
}
return colors[strings.ToLower(rgb)] // returning the empty string is ok
}

0 comments on commit 9df47df

Please sign in to comment.