From 3929c9704e9ce44ab9d031e5182b656819fb100a Mon Sep 17 00:00:00 2001 From: Michael McDonagh <51489120+m-mcdonagh@users.noreply.github.com> Date: Tue, 10 Aug 2021 15:03:34 -0400 Subject: [PATCH] Fix issue 244 (#245) Co-authored-by: makeworld <25111343+makeworld-the-better-one@users.noreply.github.com> --- config/config.go | 16 ++++++++++++--- config/default.go | 3 +++ config/theme.go | 47 +++++++++++++++++++++++++++++++++++++++++++- default-config.toml | 3 +++ display/bookmarks.go | 4 +++- display/display.go | 2 +- display/download.go | 4 ++-- display/modals.go | 8 ++++---- 8 files changed, 75 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index e7cd7d4..882edac 100644 --- a/config/config.go +++ b/config/config.go @@ -349,9 +349,19 @@ func Init() error { if !ok { return fmt.Errorf(`value for "%s" is not a string: %v`, k, v) } - color := tcell.GetColor(strings.ToLower(colorStr)) - if color == tcell.ColorDefault { - return fmt.Errorf(`invalid color format for "%s": %s`, k, colorStr) + colorStr = strings.ToLower(colorStr) + var color tcell.Color + if colorStr == "default" { + if strings.HasSuffix(k, "bg") { + color = tcell.ColorDefault + } else { + return fmt.Errorf(`"default" is only valid for a background color (color ending in "bg"), not "%s"`, k) + } + } else { + color = tcell.GetColor(colorStr) + if color == tcell.ColorDefault { + return fmt.Errorf(`invalid color format for "%s": %s`, k, colorStr) + } } SetColor(k, color) } diff --git a/config/default.go b/config/default.go index 96d968e..78fc465 100644 --- a/config/default.go +++ b/config/default.go @@ -305,6 +305,8 @@ entries_per_page = 20 # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". +# Setting a background to "default" keeps the terminal default +# If your terminal has transparency, set any background to "default" to keep it transparent # Note that not all colors will work on terminals that do not have truecolor support. # If you want to stick to the standard 16 or 256 colors, you can get @@ -323,6 +325,7 @@ entries_per_page = 20 # EXAMPLES: # hdg_1 = "green" # hdg_2 = "#5f0000" +# bg = "default" # Available keys to set: diff --git a/config/theme.go b/config/theme.go index f17e4cb..03e94bc 100644 --- a/config/theme.go +++ b/config/theme.go @@ -84,5 +84,50 @@ func GetColor(key string) tcell.Color { func GetColorString(key string) string { themeMu.RLock() defer themeMu.RUnlock() - return fmt.Sprintf("#%06x", theme[key].TrueColor().Hex()) + color := theme[key].TrueColor() + if color == tcell.ColorDefault { + return "-" + } + return fmt.Sprintf("#%06x", color.Hex()) +} + +// GetContrastingColor returns ColorBlack if color is brighter than gray +// otherwise returns ColorWhite if color is dimmer than gray +// if color is ColorDefault (undefined luminance) this returns ColorDefault +func GetContrastingColor(color tcell.Color) tcell.Color { + if color == tcell.ColorDefault { + // color should never be tcell.ColorDefault + // only config keys which end in bg are allowed to be set to default + // and the only way the argument of this function is set to ColorDefault + // is if both the text and bg of an element in the UI are set to default + return tcell.ColorDefault + } + r, g, b := color.RGB() + luminance := (77*r + 150*g + 29*b + 1<<7) >> 8 + const gray = 119 // The middle gray + if luminance > gray { + return tcell.ColorBlack + } + return tcell.ColorWhite +} + +// GetTextColor is the Same as GetColor, unless the key is "default". +// This happens on focus of a UI element which has a bg of default, in which case +// It return tcell.ColorBlack or tcell.ColorWhite, depending on which is more readable +func GetTextColor(key, bg string) tcell.Color { + themeMu.RLock() + defer themeMu.RUnlock() + color := theme[key].TrueColor() + if color != tcell.ColorDefault { + return color + } + return GetContrastingColor(theme[bg].TrueColor()) +} + +// GetTextColorString is the Same as GetColorString, unless the key is "default". +// This happens on focus of a UI element which has a bg of default, in which case +// It return tcell.ColorBlack or tcell.ColorWhite, depending on which is more readable +func GetTextColorString(key, bg string) string { + color := GetTextColor(key, bg) + return fmt.Sprintf("#%06x", color.Hex()) } diff --git a/default-config.toml b/default-config.toml index 93c3f70..be6f33f 100644 --- a/default-config.toml +++ b/default-config.toml @@ -302,6 +302,8 @@ entries_per_page = 20 # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". +# Setting a background to "default" keeps the terminal default +# If your terminal has transparency, set any background to "default" to keep it transparent # Note that not all colors will work on terminals that do not have truecolor support. # If you want to stick to the standard 16 or 256 colors, you can get @@ -320,6 +322,7 @@ entries_per_page = 20 # EXAMPLES: # hdg_1 = "green" # hdg_2 = "#5f0000" +# bg = "default" # Available keys to set: diff --git a/display/bookmarks.go b/display/bookmarks.go index 62f7803..025d3f7 100644 --- a/display/bookmarks.go +++ b/display/bookmarks.go @@ -41,8 +41,10 @@ func bkmkInit() { form.SetLabelColor(config.GetColor("bkmk_modal_label")) form.SetFieldBackgroundColor(config.GetColor("bkmk_modal_field_bg")) form.SetFieldTextColor(config.GetColor("bkmk_modal_field_text")) + form.SetFieldBackgroundColorFocused(config.GetColor("bkmk_modal_field_text")) + form.SetFieldTextColorFocused(config.GetTextColor("bkmk_modal_field_bg", "bkmk_modal_field_text")) form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) frame := m.GetFrame() frame.SetBorderColor(config.GetColor("bkmk_modal_text")) frame.SetTitleColor(config.GetColor("bkmk_modal_text")) diff --git a/display/display.go b/display/display.go index fabfb14..ab02b01 100644 --- a/display/display.go +++ b/display/display.go @@ -106,7 +106,7 @@ func Init(version, commit, builtBy string) { browser.SetTabBackgroundColor(config.GetColor("bg")) browser.SetTabBackgroundColorFocused(config.GetColor("tab_num")) browser.SetTabTextColor(config.GetColor("tab_num")) - browser.SetTabTextColorFocused(config.GetColor("bg")) + browser.SetTabTextColorFocused(config.GetTextColor("bg", "tab_num")) browser.SetTabSwitcherDivider( "", fmt.Sprintf("[%s:%s]|[-]", config.GetColorString("tab_divider"), config.GetColorString("bg")), diff --git a/display/download.go b/display/download.go index b57a097..d65c7ac 100644 --- a/display/download.go +++ b/display/download.go @@ -45,7 +45,7 @@ func dlInit() { chm.SetTextColor(config.GetColor("dl_choice_modal_text")) form := chm.GetForm() form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) frame := chm.GetFrame() frame.SetBorderColor(config.GetColor("dl_choice_modal_text")) frame.SetTitleColor(config.GetColor("dl_choice_modal_text")) @@ -56,7 +56,7 @@ func dlInit() { dlm.SetTextColor(config.GetColor("dl_modal_text")) form = dlm.GetForm() form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) frame = dlm.GetFrame() frame.SetBorderColor(config.GetColor("dl_modal_text")) frame.SetTitleColor(config.GetColor("dl_modal_text")) diff --git a/display/modals.go b/display/modals.go index bae710b..c5c1b8e 100644 --- a/display/modals.go +++ b/display/modals.go @@ -49,7 +49,7 @@ func modalInit() { m.SetTextColor(config.GetColor("info_modal_text")) form := m.GetForm() form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) frame := m.GetFrame() frame.SetBorderColor(config.GetColor("info_modal_text")) frame.SetTitleColor(config.GetColor("info_modal_text")) @@ -61,7 +61,7 @@ func modalInit() { m.SetTextColor(config.GetColor("error_modal_text")) form = m.GetForm() form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) frame = errorModal.GetFrame() frame.SetBorderColor(config.GetColor("error_modal_text")) frame.SetTitleColor(config.GetColor("error_modal_text")) @@ -78,14 +78,14 @@ func modalInit() { form.SetFieldBackgroundColor(config.GetColor("input_modal_field_bg")) form.SetFieldTextColor(config.GetColor("input_modal_field_text")) form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) m = yesNoModal m.SetButtonBackgroundColor(config.GetColor("btn_bg")) m.SetButtonTextColor(config.GetColor("btn_text")) form = m.GetForm() form.SetButtonBackgroundColorFocused(config.GetColor("btn_text")) - form.SetButtonTextColorFocused(config.GetColor("btn_bg")) + form.SetButtonTextColorFocused(config.GetTextColor("btn_bg", "btn_text")) } else { m := infoModal m.SetBackgroundColor(tcell.ColorBlack)