package ultraviolet import ( "fmt" "strings" ) func ViewToString(view View) string { parts := []string{ DisplayToString(view.Block.Display), PositionToString(view.Block.Position), FloatToString(view.Block.Float), WidthToString(view.Block.Width), HeightToString(view.Block.Height), BottomToString(view.Block.Bottom), LeftToString(view.Block.Left), TopToString(view.Block.Top), RightToString(view.Block.Right), MarginToString(view.Block.Margin), PaddingToString(view.Block.Padding), BorderThicknessToString(view.Block.Border), BorderRadiusToString(view.Block.Border), FontToString(view.Font), view.Custom, } switch view.Cursor { case CURSOR_POINTER: parts = append(parts, "cursor: pointer;") parts = append(parts, "user-select: none;") } return strings.Join(parts, "") } func DisplayToString(display int) string { switch display { case DISPLAY_BLOCK: return "display: block;" case DISPLAY_INLINE: return "display: inline-block;" } return "" } func PositionToString(position int) string { switch position { case POSITION_FIXED: return "position: fixed;" case POSITION_RELATIVE: return "position: relative;" case POSITION_ABSOLUTE: return "position: absolute;" } return "" } func FloatToString(float int) string { switch float { case FLOAT_LEFT: return "float: left;" case FLOAT_RIGHT: return "float: right;" case FLOAT_CLEAR: return "clear: both;" } return "" } func WidthToString(width int) string { if width == 0 { return "" } return fmt.Sprintf("width: %dpx;", width) } func HeightToString(height int) string { if height == 0 { return "" } return fmt.Sprintf("height: %dpx;", height) } func BottomToString(bottom int) string { if bottom == 0 { return "" } return fmt.Sprintf("bottom: %dpx;", bottom) } func LeftToString(left int) string { if left == 0 { return "" } return fmt.Sprintf("left: %dpx;", left) } func TopToString(top int) string { if top == 0 { return "" } return fmt.Sprintf("top: %dpx;", top) } func RightToString(right int) string { if right == 0 { return "" } return fmt.Sprintf("right: %dpx;", right) } func MarginToString(margin Margin) string { if margin.Top == 0 && margin.Right == 0 && margin.Bottom == 0 && margin.Left == 0 { return "" } return fmt.Sprintf( "margin: %dpx %dpx %dpx %dpx;", margin.Top, margin.Right, margin.Bottom, margin.Left, ) } func PaddingToString(padding Padding) string { if padding.Top == 0 && padding.Right == 0 && padding.Bottom == 0 && padding.Left == 0 { return "" } return fmt.Sprintf( "padding: %dpx %dpx %dpx %dpx;", padding.Top, padding.Right, padding.Bottom, padding.Left, ) } func BorderThicknessToString(border Border) string { color := ColorToString(border.Color) parts := []string{} if border.ThicknessTop != 0 { parts = append( parts, fmt.Sprintf("border-top: %dpx solid %s;", border.ThicknessTop, color), ) } if border.ThicknessRight != 0 { parts = append( parts, fmt.Sprintf("border-right: %dpx solid %s;", border.ThicknessRight, color), ) } if border.ThicknessBottom != 0 { parts = append( parts, fmt.Sprintf("border-bottom: %dpx solid %s;", border.ThicknessBottom, color), ) } if border.ThicknessLeft != 0 { parts = append( parts, fmt.Sprintf("border-left: %dpx solid %s;", border.ThicknessLeft, color), ) } return strings.Join(parts, "") } func BorderRadiusToString(border Border) string { if border.RadiusTopLeft == 0 && border.RadiusTopRight == 0 && border.RadiusBottomRight == 0 && border.RadiusBottomLeft == 0 { return "" } return fmt.Sprintf( "border-radius: %dpx %dpx %dpx %dpx;", border.RadiusTopLeft, border.RadiusTopRight, border.RadiusBottomRight, border.RadiusBottomLeft, ) } func ColorToString(color Color) string { if color.Alpha == 0 { return "" } return fmt.Sprintf( "rgba(%d, %d, %d, %.2f)", color.Red, color.Green, color.Blue, float64(color.Alpha)/255, ) } func FontToString(font Font) string { parts := []string{} if font.Face != "" { parts = append( parts, fmt.Sprintf("font-face: '%s';", font.Face), ) } if font.Size != 0 { parts = append( parts, fmt.Sprintf("font-size: %dpx;", font.Size), ) } if font.Bold { parts = append( parts, "font-weight: bold;", ) } if font.Italic { parts = append( parts, "font-view: italic;", ) } switch font.Decoration { case DECORATION_UNDERLINE: parts = append( parts, "text-decoration: underline;", ) } fontColor := ColorToString(font.FontColor) if fontColor != "" { parts = append( parts, fmt.Sprintf("color: %s;", ColorToString(font.FontColor)), ) } backgroundColor := ColorToString(font.BackgroundColor) if backgroundColor != "" { parts = append( parts, fmt.Sprintf("background-color: %s;", ColorToString(font.BackgroundColor)), ) } return strings.Join(parts, "") }