Skip to content

Commit

Permalink
Introduce functions for parsing values from ReadOne or settings callb…
Browse files Browse the repository at this point in the history
…acks
  • Loading branch information
Jacalz committed Nov 10, 2024
1 parent c1ff5c7 commit 5217ca6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
36 changes: 29 additions & 7 deletions settings/appearance/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ func GetColorScheme() (ColorScheme, error) {
return NoPreference, err
}

result := value.(uint32)
if result > 2 || result > 255 {
result = 0 // Unknown values should be treated as 0 (no preference).
}

return ColorScheme(result), nil
return ValueToColorScheme(value)
}

// GetAccentColor returns the currently set accent color.
Expand All @@ -42,7 +37,34 @@ func GetAccentColor() (*color.RGBA, error) {
return nil, ErrNotSet
}

result := value.([]float64)
return ValueToAccentColor(value)
}

// ValueToColorScheme converts a read value to a ColorScheme type.
// This is useful when for example parsing a value from the callback
// in [settings.SignalOnSettingChanged] or a value from [settings.ReadOne].
func ValueToColorScheme(value any) (ColorScheme, error) {
result, ok := value.(uint32)
if !ok {
return NoPreference, ErrNotSet
}

if result > 2 || result > 255 {
result = 0 // Unknown values should be treated as 0 (no preference).
}

return ColorScheme(result), nil
}

// ValueToAccentColor converts a read value to an accent color type.
// This is useful when for example parsing a value from the callback
// in [settings.SignalOnSettingChanged] or a value from [settings.ReadOne].
func ValueToAccentColor(value any) (*color.RGBA, error) {
result, ok := value.([]float64)
if !ok {
return nil, ErrNotSet
}

if len(result) != 4 {
return nil, ErrNotSet
}
Expand Down
13 changes: 12 additions & 1 deletion settings/appearance/contrast.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ func GetContrast() (Contrast, error) {
return NormalContrast, err
}

result := value.(uint32)
return ValueToContrast(value)
}

// ValueToContrast converts a read value to a Contrast type.
// This is useful when for example parsing a value from the callback
// in [settings.SignalOnSettingChanged] or a value from [settings.ReadOne].
func ValueToContrast(value any) (Contrast, error) {
result, ok := value.(uint32)
if !ok {
return NormalContrast, ErrNotSet
}

if result > 1 || result > 255 {
result = 0 // Unknown values should be treated as 0 (no preference).
}
Expand Down

0 comments on commit 5217ca6

Please sign in to comment.