MCPcopy
hub / github.com/g3n/engine / AttribCheckColor

Function AttribCheckColor

gui/builder.go:885–936  ·  view source on GitHub ↗

AttribCheckColor checks and converts attribute with color name or color component values

(b *Builder, am map[string]interface{}, fname string)

Source from the content-addressed store, hash-verified

883
884// AttribCheckColor checks and converts attribute with color name or color component values
885func AttribCheckColor(b *Builder, am map[string]interface{}, fname string) error {
886
887 // Checks if field is nil
888 v := am[fname]
889 if v == nil {
890 return nil
891 }
892
893 // Converts to string
894 fs, ok := v.(string)
895 if !ok {
896 return b.err(am, fname, "Not a string")
897 }
898
899 // Checks if string field is empty
900 fs = strings.Trim(fs, " ")
901 if fs == "" {
902 return nil
903 }
904
905 // If string has 1 or 2 fields it must be a color name and optional alpha
906 parts := strings.Fields(fs)
907 if len(parts) == 1 || len(parts) == 2 {
908 // First part must be a color name
909 c, ok := math32.IsColorName(parts[0])
910 if !ok {
911 return b.err(am, fname, fmt.Sprintf("Invalid color name:%s", parts[0]))
912 }
913 c4 := math32.Color4{c.R, c.G, c.B, 1}
914 if len(parts) == 2 {
915 val, err := strconv.ParseFloat(parts[1], 32)
916 if err != nil {
917 return b.err(am, fname, fmt.Sprintf("Invalid float32 value:%s", parts[1]))
918 }
919 c4.A = float32(val)
920 }
921 am[fname] = &c4
922 return nil
923 }
924
925 // Accept 3 or 4 floats values
926 va, err := b.parseFloats(am, fname, 3, 4)
927 if err != nil {
928 return err
929 }
930 if len(va) == 3 {
931 am[fname] = &math32.Color4{va[0], va[1], va[2], 1}
932 return nil
933 }
934 am[fname] = &math32.Color4{va[0], va[1], va[2], va[3]}
935 return nil
936}
937
938// AttribCheckBorderSizes checks and convert attribute with border sizes
939func AttribCheckBorderSizes(b *Builder, am map[string]interface{}, fname string) error {

Callers

nothing calls this directly

Calls 3

IsColorNameFunction · 0.92
errMethod · 0.80
parseFloatsMethod · 0.80

Tested by

no test coverage detected