Skip to content

Commit 5217ca6

Browse files
committed
Introduce functions for parsing values from ReadOne or settings callbacks
1 parent c1ff5c7 commit 5217ca6

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

settings/appearance/color.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ func GetColorScheme() (ColorScheme, error) {
2626
return NoPreference, err
2727
}
2828

29-
result := value.(uint32)
30-
if result > 2 || result > 255 {
31-
result = 0 // Unknown values should be treated as 0 (no preference).
32-
}
33-
34-
return ColorScheme(result), nil
29+
return ValueToColorScheme(value)
3530
}
3631

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

45-
result := value.([]float64)
40+
return ValueToAccentColor(value)
41+
}
42+
43+
// ValueToColorScheme converts a read value to a ColorScheme type.
44+
// This is useful when for example parsing a value from the callback
45+
// in [settings.SignalOnSettingChanged] or a value from [settings.ReadOne].
46+
func ValueToColorScheme(value any) (ColorScheme, error) {
47+
result, ok := value.(uint32)
48+
if !ok {
49+
return NoPreference, ErrNotSet
50+
}
51+
52+
if result > 2 || result > 255 {
53+
result = 0 // Unknown values should be treated as 0 (no preference).
54+
}
55+
56+
return ColorScheme(result), nil
57+
}
58+
59+
// ValueToAccentColor converts a read value to an accent color type.
60+
// This is useful when for example parsing a value from the callback
61+
// in [settings.SignalOnSettingChanged] or a value from [settings.ReadOne].
62+
func ValueToAccentColor(value any) (*color.RGBA, error) {
63+
result, ok := value.([]float64)
64+
if !ok {
65+
return nil, ErrNotSet
66+
}
67+
4668
if len(result) != 4 {
4769
return nil, ErrNotSet
4870
}

settings/appearance/contrast.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ func GetContrast() (Contrast, error) {
1717
return NormalContrast, err
1818
}
1919

20-
result := value.(uint32)
20+
return ValueToContrast(value)
21+
}
22+
23+
// ValueToContrast converts a read value to a Contrast type.
24+
// This is useful when for example parsing a value from the callback
25+
// in [settings.SignalOnSettingChanged] or a value from [settings.ReadOne].
26+
func ValueToContrast(value any) (Contrast, error) {
27+
result, ok := value.(uint32)
28+
if !ok {
29+
return NormalContrast, ErrNotSet
30+
}
31+
2132
if result > 1 || result > 255 {
2233
result = 0 // Unknown values should be treated as 0 (no preference).
2334
}

0 commit comments

Comments
 (0)