Added ColorPair type

This commit is contained in:
Alexei Anoshenko 2026-07-09 21:36:44 +03:00
parent 38c914e2ea
commit 1bce3fea36
12 changed files with 147 additions and 58 deletions

View File

@ -10,6 +10,7 @@
* Added "outside-color" add "outside-filter" properties to Popup interface * Added "outside-color" add "outside-filter" properties to Popup interface
* Added ViewSeq add ViewCount methods to ParentView interface * Added ViewSeq add ViewCount methods to ParentView interface
* Added ToBoundsProperty method to Bounds struct * Added ToBoundsProperty method to Bounds struct
* Added ColorPair type
# v0.20.0 # v0.20.0

View File

@ -45,6 +45,9 @@ func (point *BackgroundGradientAngle) String() string {
case Color: case Color:
result = color.String() result = color.String()
case ColorPair:
result = color.String()
} }
} }
@ -65,17 +68,22 @@ func (point *BackgroundGradientAngle) color(session Session) (Color, bool) {
if point.Color != nil { if point.Color != nil {
switch color := point.Color.(type) { switch color := point.Color.(type) {
case string: case string:
if ok, constName := isConstantName(color); ok { if lightColor, darkColor, ok := valueToColor(color, session); ok {
if clr, ok := session.Color(constName); ok { if session.DarkTheme() {
return clr, true return darkColor, true
} }
} else if clr, ok := StringToColor(color); ok { return lightColor, true
return clr, true
} }
case Color: case Color:
return color, true return color, true
case ColorPair:
if session.DarkTheme() {
return color.Dark, true
}
return color.Light, true
default: default:
if n, ok := isInt(color); ok { if n, ok := isInt(color); ok {
return Color(n), true return Color(n), true
@ -198,7 +206,7 @@ func backgroundConicGradientSet(properties Properties, tag PropertyName, value a
} }
func (gradient *backgroundConicGradient) stringToGradientPoint(text string) (BackgroundGradientAngle, bool) { func (gradient *backgroundConicGradient) stringToGradientPoint(text string) (BackgroundGradientAngle, bool) {
var result BackgroundGradientAngle result := BackgroundGradientAngle{Color: nil}
colorText := "" colorText := ""
pointText := "" pointText := ""
@ -209,15 +217,20 @@ func (gradient *backgroundConicGradient) stringToGradientPoint(text string) (Bac
colorText = text colorText = text
} }
if colorText == "" { if colorText != "" {
return result, false
}
if ok, _ := isConstantName(colorText); ok { if ok, _ := isConstantName(colorText); ok {
result.Color = colorText result.Color = colorText
} else if color, ok := StringToColor(colorText); ok { } else if color, err := stringToColor(colorText); err == nil {
result.Color = color result.Color = color
} else { } else if obj, err := ParseDataText(colorText); err == nil {
if colorPair, ok := parseColorPair(obj); ok {
result.Color = colorPair
}
}
}
if result.Color == nil {
ErrorLogF(`Invalid color value: "%s"`, colorText)
return result, false return result, false
} }

View File

@ -163,15 +163,20 @@ func (point *BackgroundGradientPoint) setValue(text string) bool {
pointText = strings.Trim(text[index+1:], " ") pointText = strings.Trim(text[index+1:], " ")
} }
if colorText == "" { if colorText != "" {
return false
}
if ok, _ := isConstantName(colorText); ok { if ok, _ := isConstantName(colorText); ok {
point.Color = colorText point.Color = colorText
} else if color, ok := StringToColor(colorText); ok { } else if color, err := stringToColor(colorText); err == nil {
point.Color = color point.Color = color
} else { } else if obj, err := ParseDataText(colorText); err == nil {
if colorPair, ok := parseColorPair(obj); ok {
point.Color = colorPair
}
}
}
if point.Color == nil {
ErrorLogF(`Invalid color value: "%s"`, colorText)
return false return false
} }
@ -200,6 +205,12 @@ func (point *BackgroundGradientPoint) color(session Session) (Color, bool) {
case Color: case Color:
return color, true return color, true
case ColorPair:
if session.DarkTheme() {
return color.Dark, true
}
return color.Light, true
default: default:
if n, ok := isInt(point.Color); ok { if n, ok := isInt(point.Color); ok {
return Color(n), true return Color(n), true
@ -219,6 +230,9 @@ func (point *BackgroundGradientPoint) String() string {
case Color: case Color:
result = color.String() result = color.String()
case ColorPair:
result = color.String()
} }
} }

View File

@ -134,7 +134,7 @@ const (
// Used by BorderProperty. // Used by BorderProperty.
// Left border line color. // Left border line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -145,7 +145,7 @@ const (
// Used by BorderProperty. // Used by BorderProperty.
// Right border line color. // Right border line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -156,7 +156,7 @@ const (
// Used by BorderProperty. // Used by BorderProperty.
// Top border line color. // Top border line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -167,7 +167,7 @@ const (
// Used by BorderProperty. // Used by BorderProperty.
// Bottom border line color. // Bottom border line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.

View File

@ -8,9 +8,18 @@ import (
"strings" "strings"
) )
// Color - represent color in argb format // Color represents the color in argb format
type Color uint32 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 // ARGB creates Color using alpha, red, green and blue components
func ARGB[T int | uint | int8 | uint8](alpha, red, green, blue T) Color { func ARGB[T int | uint | int8 | uint8](alpha, red, green, blue T) Color {
return ((Color(alpha) & 0xFF) << 24) + return ((Color(alpha) & 0xFF) << 24) +
@ -211,3 +220,38 @@ func writeColorCSS(buffer *strings.Builder, lightColor, darkColor Color, session
buffer.WriteString(lightColor.cssString()) 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())
}

View File

@ -83,7 +83,7 @@ const (
// Used by ColumnLayout. // Used by ColumnLayout.
// Set the color of the line drawn between columns in a multi-column layout. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.

View File

@ -223,7 +223,7 @@ const (
// Used by Popup. // Used by Popup.
// Specifies the color used to paint the space outside and below the 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.

View File

@ -87,18 +87,29 @@ func valueToColor(value any, session Session) (Color, Color, bool) {
case Color: case Color:
return value, value, true return value, value, true
case ColorPair:
return value.Light, value.Dark, true
case string: case string:
if ok, constName := isConstantName(value); ok { if ok, constName := isConstantName(value); ok {
lightColor, ok1 := session.getColor(constName, false) lightColor, ok1 := session.getColor(constName, false)
darkColor, ok2 := session.getColor(constName, true) darkColor, ok2 := session.getColor(constName, true)
return lightColor, darkColor, ok1 && ok2 return lightColor, darkColor, ok1 && ok2
} }
if color, ok := StringToColor(value); ok {
if color, err := stringToColor(value); err == nil {
return color, color, true 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 return Color(0), Color(0), false
} }

View File

@ -578,7 +578,7 @@ const (
// Used by View. // Used by View.
// Sets the accent color for UI controls generated by some elements. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -589,7 +589,7 @@ const (
// Used by View. // Used by View.
// Set the background color of a 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -864,7 +864,7 @@ const (
// Used by View. // Used by View.
// Set the line color for all four sides of a view's border. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -875,7 +875,7 @@ const (
// Used by View. // Used by View.
// Set the line color of a view's left border. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -886,7 +886,7 @@ const (
// Used by View. // Used by View.
// Set the line color of a view's right border. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -897,7 +897,7 @@ const (
// Used by View. // Used by View.
// Set the line color of a view's top border. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -908,7 +908,7 @@ const (
// Used by View. // Used by View.
// Set the line color of a view's bottom border. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.
@ -945,7 +945,7 @@ const (
// Used by View. // Used by View.
// Set the color of an view's outline. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // 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 // 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. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // 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 // 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. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // 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 // 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. // 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. // Internal type is Color, other types converted to it during assignment.
// See [Color] description for more details. // See [Color] description for more details.

View File

@ -23,16 +23,6 @@ var colorProperties = []PropertyName{
AccentColor, AccentColor,
} }
/*
func isPropertyInList(tag PropertyName, list []PropertyName) bool {
for _, prop := range list {
if prop == tag {
return true
}
}
return false
}
*/
var angleProperties = []PropertyName{ var angleProperties = []PropertyName{
From, From,
} }
@ -720,12 +710,28 @@ func setColorProperty(properties Properties, tag PropertyName, value any) []Prop
case string: case string:
var err error var err error
if result, err = stringToColor(value); err != nil { 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) invalidPropertyValue(tag, value)
return nil return nil
} }
case Color: case Color:
result = value 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: default:
if color, ok := isInt(value); ok { if color, ok := isInt(value); ok {
result = Color(color) result = Color(color)

View File

@ -15,7 +15,7 @@ const (
// //
// Line color. // Line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -24,7 +24,7 @@ const (
// //
// Border line color. // Border line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -33,7 +33,7 @@ const (
// //
// Outline line color. // Outline line color.
// //
// Supported types: Color, string. // Supported types: Color, ColorPair, string.
// //
// Internal type is Color, other types converted to it during assignment. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -42,7 +42,7 @@ const (
// //
// Color property of the shadow. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.

View File

@ -386,7 +386,7 @@ const (
// Used by TableView. // Used by TableView.
// Set the line color for all four sides of a table cell's border. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -397,7 +397,7 @@ const (
// Used by TableView. // Used by TableView.
// Set the line color of a table cell's left border. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -408,7 +408,7 @@ const (
// Used by TableView. // Used by TableView.
// Set the line color of a table cell's right border. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -419,7 +419,7 @@ const (
// Used by TableView. // Used by TableView.
// Set the line color of a table cell's top border. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.
@ -430,7 +430,7 @@ const (
// Used by TableView. // Used by TableView.
// Set the line color of a table cell's bottom border. // 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. // Internal type is Color, other types converted to it during assignment.
// See Color description for more details. // See Color description for more details.