Skip to content

Fix issue 244 #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 10, 2021
16 changes: 13 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -323,6 +325,7 @@ entries_per_page = 20
# EXAMPLES:
# hdg_1 = "green"
# hdg_2 = "#5f0000"
# bg = "default"

# Available keys to set:

Expand Down
47 changes: 46 additions & 1 deletion config/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
3 changes: 3 additions & 0 deletions default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -320,6 +322,7 @@ entries_per_page = 20
# EXAMPLES:
# hdg_1 = "green"
# hdg_2 = "#5f0000"
# bg = "default"

# Available keys to set:

Expand Down
4 changes: 3 additions & 1 deletion display/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this function call, and all the others in this PR, incorrect? The function signature for GetTextColor is GetTextColor(key, bg string) tcell.Color, which would indicate having "btn_bg" second and "btn_text" first. Or am I misunderstanding?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for focused UI elements, the text color and bg color swap. When focused, the button has text color of whatever "btn_bg" is set to, and a bg of whatever color "btn_text" is set to.

Because "default" is not a valid text color in the config, this function is always called with a key ending with bg as its key, and a bg ending with text.

I tried to be as concise as possible with the naming. Maybe SwapColor(new_text_key, new_bg_key string) or something similar would be less confusing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it's good the way it is then, I just misunderstood.

frame := m.GetFrame()
frame.SetBorderColor(config.GetColor("bkmk_modal_text"))
frame.SetTitleColor(config.GetColor("bkmk_modal_text"))
Expand Down
2 changes: 1 addition & 1 deletion display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
4 changes: 2 additions & 2 deletions display/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down
8 changes: 4 additions & 4 deletions display/modals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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)
Expand Down