From 1bce3fea36da55e50f5f79374b188d51213f1ffa Mon Sep 17 00:00:00 2001 From: Alexei Anoshenko <2277098+anoshenko@users.noreply.github.com> Date: Thu, 9 Jul 2026 21:36:44 +0300 Subject: [PATCH] Added ColorPair type --- CHANGELOG.md | 1 + backgroundConicGradient.go | 39 ++++++++++++++++++++----------- backgroundLinearGradient.go | 28 ++++++++++++++++------ border.go | 8 +++---- color.go | 46 ++++++++++++++++++++++++++++++++++++- columnLayout.go | 2 +- popup.go | 2 +- propertyGet.go | 13 ++++++++++- propertyNames.go | 22 +++++++++--------- propertySet.go | 26 +++++++++++++-------- shadow.go | 8 +++---- tableView.go | 10 ++++---- 12 files changed, 147 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c034f97..f40ff12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Added "outside-color" add "outside-filter" properties to Popup interface * Added ViewSeq add ViewCount methods to ParentView interface * Added ToBoundsProperty method to Bounds struct +* Added ColorPair type # v0.20.0 diff --git a/backgroundConicGradient.go b/backgroundConicGradient.go index 2162724..1534ebd 100644 --- a/backgroundConicGradient.go +++ b/backgroundConicGradient.go @@ -45,6 +45,9 @@ func (point *BackgroundGradientAngle) String() string { case Color: result = color.String() + + case ColorPair: + result = color.String() } } @@ -65,17 +68,22 @@ func (point *BackgroundGradientAngle) color(session Session) (Color, bool) { if point.Color != nil { switch color := point.Color.(type) { case string: - if ok, constName := isConstantName(color); ok { - if clr, ok := session.Color(constName); ok { - return clr, true + if lightColor, darkColor, ok := valueToColor(color, session); ok { + if session.DarkTheme() { + return darkColor, true } - } else if clr, ok := StringToColor(color); ok { - return clr, true + return lightColor, true } case Color: return color, true + case ColorPair: + if session.DarkTheme() { + return color.Dark, true + } + return color.Light, true + default: if n, ok := isInt(color); ok { return Color(n), true @@ -198,7 +206,7 @@ func backgroundConicGradientSet(properties Properties, tag PropertyName, value a } func (gradient *backgroundConicGradient) stringToGradientPoint(text string) (BackgroundGradientAngle, bool) { - var result BackgroundGradientAngle + result := BackgroundGradientAngle{Color: nil} colorText := "" pointText := "" @@ -209,15 +217,20 @@ func (gradient *backgroundConicGradient) stringToGradientPoint(text string) (Bac colorText = text } - if colorText == "" { - return result, false + if colorText != "" { + if ok, _ := isConstantName(colorText); ok { + result.Color = colorText + } else if color, err := stringToColor(colorText); err == nil { + result.Color = color + } else if obj, err := ParseDataText(colorText); err == nil { + if colorPair, ok := parseColorPair(obj); ok { + result.Color = colorPair + } + } } - if ok, _ := isConstantName(colorText); ok { - result.Color = colorText - } else if color, ok := StringToColor(colorText); ok { - result.Color = color - } else { + if result.Color == nil { + ErrorLogF(`Invalid color value: "%s"`, colorText) return result, false } diff --git a/backgroundLinearGradient.go b/backgroundLinearGradient.go index a1a0b3e..4d8b71a 100644 --- a/backgroundLinearGradient.go +++ b/backgroundLinearGradient.go @@ -163,15 +163,20 @@ func (point *BackgroundGradientPoint) setValue(text string) bool { pointText = strings.Trim(text[index+1:], " ") } - if colorText == "" { - return false + if colorText != "" { + if ok, _ := isConstantName(colorText); ok { + point.Color = colorText + } else if color, err := stringToColor(colorText); err == nil { + point.Color = color + } else if obj, err := ParseDataText(colorText); err == nil { + if colorPair, ok := parseColorPair(obj); ok { + point.Color = colorPair + } + } } - if ok, _ := isConstantName(colorText); ok { - point.Color = colorText - } else if color, ok := StringToColor(colorText); ok { - point.Color = color - } else { + if point.Color == nil { + ErrorLogF(`Invalid color value: "%s"`, colorText) return false } @@ -200,6 +205,12 @@ func (point *BackgroundGradientPoint) color(session Session) (Color, bool) { case Color: return color, true + case ColorPair: + if session.DarkTheme() { + return color.Dark, true + } + return color.Light, true + default: if n, ok := isInt(point.Color); ok { return Color(n), true @@ -219,6 +230,9 @@ func (point *BackgroundGradientPoint) String() string { case Color: result = color.String() + + case ColorPair: + result = color.String() } } diff --git a/border.go b/border.go index a6950b6..7b0316e 100644 --- a/border.go +++ b/border.go @@ -134,7 +134,7 @@ const ( // Used by BorderProperty. // Left border line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -145,7 +145,7 @@ const ( // Used by BorderProperty. // Right border line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -156,7 +156,7 @@ const ( // Used by BorderProperty. // Top border line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -167,7 +167,7 @@ const ( // Used by BorderProperty. // Bottom border line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. diff --git a/color.go b/color.go index a3dcec2..e87ab68 100644 --- a/color.go +++ b/color.go @@ -8,9 +8,18 @@ import ( "strings" ) -// Color - represent color in argb format +// Color represents the color in argb format type Color uint32 +// ColorPair represents the color pair in argb format +type ColorPair struct { + // Light is the color used in the light theme + Light Color + + // Dark is the color used in the light theme + Dark Color +} + // ARGB creates Color using alpha, red, green and blue components func ARGB[T int | uint | int8 | uint8](alpha, red, green, blue T) Color { return ((Color(alpha) & 0xFF) << 24) + @@ -211,3 +220,38 @@ func writeColorCSS(buffer *strings.Builder, lightColor, darkColor Color, session buffer.WriteString(lightColor.cssString()) } } + +func parseColorPair(obj DataObject) (ColorPair, bool) { + + var err error + result := ColorPair{} + + for property := range obj.Properties() { + if property.Type() != TextNode { + return ColorPair{}, false + } + + switch property.Tag() { + case "light": + result.Light, err = stringToColor(property.Text()) + if err != nil { + return ColorPair{}, false + } + + case "dark": + result.Dark, err = stringToColor(property.Text()) + if err != nil { + return ColorPair{}, false + } + + default: + return ColorPair{}, false + } + } + + return result, true +} + +func (pair ColorPair) String() string { + return fmt.Sprintf("_{ light = %s, dark = %s }", pair.Light.String(), pair.Dark.String()) +} diff --git a/columnLayout.go b/columnLayout.go index 9cfad54..5ad864a 100644 --- a/columnLayout.go +++ b/columnLayout.go @@ -83,7 +83,7 @@ const ( // Used by ColumnLayout. // Set the color of the line drawn between columns in a multi-column layout. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. diff --git a/popup.go b/popup.go index 2f485cb..4ca1cdc 100644 --- a/popup.go +++ b/popup.go @@ -223,7 +223,7 @@ const ( // Used by Popup. // Specifies the color used to paint the space outside and below the Popup. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. diff --git a/propertyGet.go b/propertyGet.go index a7d3785..c8e493e 100644 --- a/propertyGet.go +++ b/propertyGet.go @@ -87,18 +87,29 @@ func valueToColor(value any, session Session) (Color, Color, bool) { case Color: return value, value, true + case ColorPair: + return value.Light, value.Dark, true + case string: if ok, constName := isConstantName(value); ok { lightColor, ok1 := session.getColor(constName, false) darkColor, ok2 := session.getColor(constName, true) return lightColor, darkColor, ok1 && ok2 } - if color, ok := StringToColor(value); ok { + + if color, err := stringToColor(value); err == nil { return color, color, true } + + if obj, err := ParseDataText(value); err == nil { + if colorPair, ok := parseColorPair(obj); ok { + return colorPair.Light, colorPair.Dark, true + } + } } } + ErrorLogF(`Invalid color value: "%v"`, value) return Color(0), Color(0), false } diff --git a/propertyNames.go b/propertyNames.go index 573e037..182c01c 100644 --- a/propertyNames.go +++ b/propertyNames.go @@ -578,7 +578,7 @@ const ( // Used by View. // Sets the accent color for UI controls generated by some elements. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -589,7 +589,7 @@ const ( // Used by View. // Set the background color of a view. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -864,7 +864,7 @@ const ( // Used by View. // Set the line color for all four sides of a view's border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -875,7 +875,7 @@ const ( // Used by View. // Set the line color of a view's left border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -886,7 +886,7 @@ const ( // Used by View. // Set the line color of a view's right border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -897,7 +897,7 @@ const ( // Used by View. // Set the line color of a view's top border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -908,7 +908,7 @@ const ( // Used by View. // Set the line color of a view's bottom border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -945,7 +945,7 @@ const ( // Used by View. // Set the color of an view's outline. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -1006,7 +1006,7 @@ const ( // Set the foreground color value of a view's text and text decorations. This is an inherited property, i.e. if it is not // defined, then the value of the parent view is used. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -1123,7 +1123,7 @@ const ( // Sets the color of the lines specified by "strikethrough", "overline" and "underline" properties. This is an inherited // property, i.e. if it is not defined, then the value of the parent view is used. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. @@ -2071,7 +2071,7 @@ const ( // Sets the color of the insertion caret, the visible marker where the next character typed will be inserted. This is // sometimes referred to as the text input cursor. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See [Color] description for more details. diff --git a/propertySet.go b/propertySet.go index 9e56a34..5fbf6ce 100644 --- a/propertySet.go +++ b/propertySet.go @@ -23,16 +23,6 @@ var colorProperties = []PropertyName{ AccentColor, } -/* - func isPropertyInList(tag PropertyName, list []PropertyName) bool { - for _, prop := range list { - if prop == tag { - return true - } - } - return false - } -*/ var angleProperties = []PropertyName{ From, } @@ -720,12 +710,28 @@ func setColorProperty(properties Properties, tag PropertyName, value any) []Prop case string: var err error if result, err = stringToColor(value); err != nil { + if obj, err := ParseDataText(value); err == nil { + if colorPair, ok := parseColorPair(obj); ok { + return setPropertyValue(properties, tag, colorPair) + } + } invalidPropertyValue(tag, value) return nil } + case Color: result = value + case ColorPair: + return setPropertyValue(properties, tag, value) + + case DataObject: + if colorPair, ok := parseColorPair(value); ok { + return setPropertyValue(properties, tag, colorPair) + } + invalidPropertyValue(tag, value) + return nil + default: if color, ok := isInt(value); ok { result = Color(color) diff --git a/shadow.go b/shadow.go index 0853799..697f4c5 100644 --- a/shadow.go +++ b/shadow.go @@ -15,7 +15,7 @@ const ( // // Line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -24,7 +24,7 @@ const ( // // Border line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -33,7 +33,7 @@ const ( // // Outline line color. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -42,7 +42,7 @@ const ( // // Color property of the shadow. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. diff --git a/tableView.go b/tableView.go index 50344cd..4523560 100644 --- a/tableView.go +++ b/tableView.go @@ -386,7 +386,7 @@ const ( // Used by TableView. // Set the line color for all four sides of a table cell's border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -397,7 +397,7 @@ const ( // Used by TableView. // Set the line color of a table cell's left border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -408,7 +408,7 @@ const ( // Used by TableView. // Set the line color of a table cell's right border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -419,7 +419,7 @@ const ( // Used by TableView. // Set the line color of a table cell's top border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details. @@ -430,7 +430,7 @@ const ( // Used by TableView. // Set the line color of a table cell's bottom border. // - // Supported types: Color, string. + // Supported types: Color, ColorPair, string. // // Internal type is Color, other types converted to it during assignment. // See Color description for more details.