From 94983b0c61eab5c39df2a7474096136669227166 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 30 Apr 2023 16:49:55 +0200 Subject: [PATCH] add customization for body color --- internal/converter/converter.go | 13 +++++++++++++ internal/converter/options/converter_options.go | 6 ++++++ internal/epub/epub.go | 3 +-- internal/epub/options/epub_options.go | 5 +++++ .../epub/templates/epub_templates_style.css.tmpl | 8 ++++---- main.go | 4 ++++ 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 551631c..7ba6e1e 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "runtime" "strings" "time" @@ -116,6 +117,8 @@ func (c *Converter) InitParse() { c.AddIntParam(&c.Options.LimitMb, "limitmb", c.Options.LimitMb, "Limit size of the EPUB: Default nolimit (0), Minimum 20") c.AddBoolParam(&c.Options.StripFirstDirectoryFromToc, "strip", c.Options.StripFirstDirectoryFromToc, "Strip first directory from the TOC if only 1") c.AddIntParam(&c.Options.SortPathMode, "sort", c.Options.SortPathMode, "Sort path mode\n0 = alpha for path and file\n1 = alphanum for path and alpha for file\n2 = alphanum for path and file") + c.AddStringParam(&c.Options.ForegroundColor, "foreground-color", c.Options.ForegroundColor, "Foreground color in hexa format RGB. Black=000, White=FFF") + c.AddStringParam(&c.Options.BackgroundColor, "background-color", c.Options.BackgroundColor, "Background color in hexa format RGB. Black=000, White=FFF, Light Gray=DDD, Dark Gray=777") c.AddSection("Default config") c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters") @@ -293,6 +296,16 @@ func (c *Converter) Validate() error { return errors.New("sort should be 0, 1 or 2") } + // Color + colorRegex := regexp.MustCompile("^[0-9A-F]{3}$") + if !colorRegex.MatchString(c.Options.ForegroundColor) { + return errors.New("foreground color must have color format in hexa: [0-9A-F]{3}") + } + + if !colorRegex.MatchString(c.Options.BackgroundColor) { + return errors.New("background color must have color format in hexa: [0-9A-F]{3}") + } + return nil } diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index 39b857b..d121d96 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -39,6 +39,8 @@ type Options struct { LimitMb int `yaml:"limit_mb"` StripFirstDirectoryFromToc bool `yaml:"strip_first_directory_from_toc"` SortPathMode int `yaml:"sort_path_mode"` + ForegroundColor string `yaml:"foreground_color"` + BackgroundColor string `yaml:"background_color"` // Default Config Show bool `yaml:"-"` @@ -77,6 +79,8 @@ func New() *Options { LimitMb: 0, StripFirstDirectoryFromToc: false, SortPathMode: 1, + ForegroundColor: "000", + BackgroundColor: "FFF", profiles: profiles.New(), } } @@ -182,6 +186,8 @@ func (o *Options) ShowConfig() string { {"LimitMb", limitmb}, {"StripFirstDirectoryFromToc", o.StripFirstDirectoryFromToc}, {"SortPathMode", sortpathmode}, + {"Foreground Color", fmt.Sprintf("#%s", o.ForegroundColor)}, + {"Background Color", fmt.Sprintf("#%s", o.BackgroundColor)}, } { b.WriteString(fmt.Sprintf("\n %-26s: %v", v.K, v.V)) } diff --git a/internal/epub/epub.go b/internal/epub/epub.go index abf1b45..d833a3d 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -273,8 +273,7 @@ func (e *ePub) Write() error { })}, {"OEBPS/toc.xhtml", epubtemplates.Toc(title, e.StripFirstDirectoryFromToc, part.Images)}, {"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{ - "PageWidth": e.Image.View.Width, - "PageHeight": e.Image.View.Height, + "View": e.Image.View, })}, {"OEBPS/Text/space_title.xhtml", e.render(epubtemplates.Blank, map[string]any{ "Title": "Blank Page Title", diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index 69f4e98..8f14be1 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -10,8 +10,13 @@ type Crop struct { Left, Up, Right, Bottom int } +type Color struct { + Foreground, Background string +} + type View struct { Width, Height int + Color Color } type Image struct { diff --git a/internal/epub/templates/epub_templates_style.css.tmpl b/internal/epub/templates/epub_templates_style.css.tmpl index 7059093..cfa4f08 100644 --- a/internal/epub/templates/epub_templates_style.css.tmpl +++ b/internal/epub/templates/epub_templates_style.css.tmpl @@ -1,12 +1,12 @@ body { - color: #000; - background: #FFF; + color: #{{ .View.Color.Foreground }}; + background: #{{ .View.Color.Background }}; top: 0; left: 0; margin: 0; padding: 0; - width: {{ .PageWidth }}px; - height: {{ .PageHeight }}px; + width: {{ .View.Width }}px; + height: {{ .View.Height }}px; text-align: center; } diff --git a/main.go b/main.go index cd8e378..8a571d3 100644 --- a/main.go +++ b/main.go @@ -132,6 +132,10 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s View: &epuboptions.View{ Width: perfectWidth, Height: perfectHeight, + Color: epuboptions.Color{ + Foreground: cmd.Options.ForegroundColor, + Background: cmd.Options.BackgroundColor, + }, }, }, }).Write(); err != nil {