rui_orig/backgroundConicGradient.go

342 lines
7.6 KiB
Go
Raw Permalink Normal View History

2022-04-30 12:13:16 +03:00
package rui
import (
"strings"
)
type backgroundConicGradient struct {
backgroundElement
}
// BackgroundGradientAngle defined an element of the conic gradient
type BackgroundGradientAngle struct {
// Color - the color of the key angle. Must not be nil.
// Can take a value of Color type or string (color constant or textual description of the color)
2022-07-26 18:36:00 +03:00
Color any
2022-04-30 12:13:16 +03:00
// Angle - the key angle. Optional (may be nil).
// Can take a value of AngleUnit type or string (angle constant or textual description of the angle)
2022-07-26 18:36:00 +03:00
Angle any
2022-04-30 12:13:16 +03:00
}
// NewBackgroundConicGradient creates the new background conic gradient
func NewBackgroundConicGradient(params Params) BackgroundElement {
result := new(backgroundConicGradient)
2024-11-13 12:56:39 +03:00
result.init()
2022-04-30 12:13:16 +03:00
for tag, value := range params {
result.Set(tag, value)
}
return result
}
// String convert internal representation of [BackgroundGradientAngle] into a string.
2022-04-30 12:13:16 +03:00
func (point *BackgroundGradientAngle) String() string {
result := "black"
if point.Color != nil {
switch color := point.Color.(type) {
case string:
result = color
case Color:
result = color.String()
}
}
if point.Angle != nil {
switch value := point.Angle.(type) {
case string:
result += " " + value
case AngleUnit:
result += " " + value.String()
}
}
return result
}
func (point *BackgroundGradientAngle) color(session Session) (Color, bool) {
if point.Color != nil {
switch color := point.Color.(type) {
case string:
if color != "" {
if color[0] == '@' {
if clr, ok := session.Color(color[1:]); ok {
return clr, true
}
} else {
if clr, ok := StringToColor(color); ok {
return clr, true
}
}
}
case Color:
return color, true
}
}
return 0, false
}
func (point *BackgroundGradientAngle) isValid(session Session) bool {
_, ok := point.color(session)
return ok
}
func (point *BackgroundGradientAngle) cssString(session Session, buffer *strings.Builder) {
if color, ok := point.color(session); ok {
buffer.WriteString(color.cssString())
} else {
return
}
if point.Angle != nil {
switch value := point.Angle.(type) {
case string:
if value != "" {
if value[0] == '@' {
if val, ok := session.Constant(value[1:]); ok {
value = val
} else {
return
}
}
if angle, ok := StringToAngleUnit(value); ok {
buffer.WriteRune(' ')
buffer.WriteString(angle.cssString())
}
}
case AngleUnit:
buffer.WriteRune(' ')
buffer.WriteString(value.cssString())
}
}
}
2024-11-13 12:56:39 +03:00
func (gradient *backgroundConicGradient) init() {
gradient.backgroundElement.init()
gradient.normalize = normalizeConicGradientTag
gradient.set = backgroundConicGradientSet
gradient.supportedProperties = []PropertyName{
CenterX, CenterY, Repeating, From, Gradient,
}
}
2022-04-30 12:13:16 +03:00
func (gradient *backgroundConicGradient) Tag() string {
return "conic-gradient"
}
func (image *backgroundConicGradient) Clone() BackgroundElement {
result := NewBackgroundConicGradient(nil)
for tag, value := range image.properties {
result.setRaw(tag, value)
}
return result
}
2024-11-13 12:56:39 +03:00
func normalizeConicGradientTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
2022-04-30 12:13:16 +03:00
switch tag {
case "x-center":
tag = CenterX
case "y-center":
tag = CenterY
}
return tag
}
2024-11-13 12:56:39 +03:00
func backgroundConicGradientSet(properties Properties, tag PropertyName, value any) []PropertyName {
2022-04-30 12:13:16 +03:00
switch tag {
case Gradient:
2024-11-13 12:56:39 +03:00
switch value := value.(type) {
case string:
if value == "" {
return propertiesRemove(properties, tag)
}
if strings.Contains(value, ",") || strings.Contains(value, " ") {
if vector := parseGradientText(value); vector != nil {
properties.setRaw(Gradient, vector)
return []PropertyName{tag}
}
} else if isConstantName(value) {
properties.setRaw(Gradient, value)
return []PropertyName{tag}
}
ErrorLogF(`Invalid conic gradient: "%s"`, value)
case []BackgroundGradientAngle:
count := len(value)
if count < 2 {
ErrorLog("The gradient must contain at least 2 points")
return nil
}
for i, point := range value {
if point.Color == nil {
ErrorLogF("Invalid %d element of the conic gradient: Color is nil", i)
return nil
}
}
properties.setRaw(Gradient, value)
return []PropertyName{tag}
default:
notCompatibleType(tag, value)
}
return nil
2022-04-30 12:13:16 +03:00
}
2024-11-13 12:56:39 +03:00
return propertiesSet(properties, tag, value)
2022-04-30 12:13:16 +03:00
}
2022-07-26 18:36:00 +03:00
func (gradient *backgroundConicGradient) stringToAngle(text string) (any, bool) {
2022-04-30 12:13:16 +03:00
if text == "" {
return nil, false
} else if text[0] == '@' {
return text, true
}
return StringToAngleUnit(text)
}
func (gradient *backgroundConicGradient) stringToGradientPoint(text string) (BackgroundGradientAngle, bool) {
var result BackgroundGradientAngle
colorText := ""
pointText := ""
if index := strings.Index(text, " "); index > 0 {
colorText = text[:index]
pointText = strings.Trim(text[index+1:], " ")
} else {
colorText = text
}
if colorText == "" {
return result, false
}
if colorText[0] == '@' {
result.Color = colorText
} else if color, ok := StringToColor(colorText); ok {
result.Color = color
} else {
return result, false
}
if pointText != "" {
if angle, ok := gradient.stringToAngle(pointText); ok {
result.Angle = angle
} else {
return result, false
}
}
return result, true
}
func (gradient *backgroundConicGradient) parseGradientText(value string) []BackgroundGradientAngle {
elements := strings.Split(value, ",")
count := len(elements)
if count < 2 {
ErrorLog("The gradient must contain at least 2 points")
return nil
}
vector := make([]BackgroundGradientAngle, count)
for i, element := range elements {
var ok bool
if vector[i], ok = gradient.stringToGradientPoint(strings.Trim(element, " ")); !ok {
2022-11-23 15:10:29 +03:00
ErrorLogF(`Invalid %d element of the conic gradient: "%s"`, i, element)
2022-04-30 12:13:16 +03:00
return nil
}
}
return vector
}
func (gradient *backgroundConicGradient) cssStyle(session Session) string {
points := []BackgroundGradientAngle{}
if value, ok := gradient.properties[Gradient]; ok {
switch value := value.(type) {
case string:
if text, ok := session.resolveConstants(value); ok && text != "" {
if points = gradient.parseGradientText(text); points == nil {
return ""
}
} else {
ErrorLog(`Invalid conic gradient: ` + value)
return ""
}
case []BackgroundGradientAngle:
points = value
}
} else {
return ""
}
if len(points) < 2 {
ErrorLog("The gradient must contain at least 2 points")
return ""
}
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
if repeating, _ := boolProperty(gradient, Repeating, session); repeating {
buffer.WriteString(`repeating-conic-gradient(`)
} else {
buffer.WriteString(`conic-gradient(`)
}
comma := false
if angle, ok := angleProperty(gradient, From, session); ok {
buffer.WriteString("from ")
buffer.WriteString(angle.cssString())
comma = true
}
x, _ := sizeProperty(gradient, CenterX, session)
y, _ := sizeProperty(gradient, CenterX, session)
if x.Type != Auto || y.Type != Auto {
if comma {
buffer.WriteRune(' ')
}
buffer.WriteString("at ")
buffer.WriteString(x.cssString("50%", session))
2022-04-30 12:13:16 +03:00
buffer.WriteString(" ")
buffer.WriteString(y.cssString("50%", session))
2022-04-30 12:13:16 +03:00
comma = true
}
for _, point := range points {
if point.isValid(session) {
if comma {
buffer.WriteString(`, `)
}
point.cssString(session, buffer)
comma = true
}
}
buffer.WriteString(") ")
return buffer.String()
}
2024-04-22 19:14:58 +03:00
func (gradient *backgroundConicGradient) writeString(buffer *strings.Builder, indent string) {
2024-11-13 12:56:39 +03:00
gradient.writeToBuffer(buffer, indent, gradient.Tag(), []PropertyName{
2024-04-22 19:14:58 +03:00
Gradient,
CenterX,
CenterY,
Repeating,
})
}
func (gradient *backgroundConicGradient) String() string {
2024-04-22 20:03:40 +03:00
return runStringWriter(gradient)
2024-04-22 19:14:58 +03:00
}