siyuan-note/siyuan · error

invalid foreground color [%s]

Error message

invalid foreground color [%s]

What it means

normalizeAttributeViewColorTheme validates a color theme's foreground (Color) value against attributeViewColorPattern after trimming and lowercasing. If it does not match, the original (untrimmed) value is reported in the error. This guards the attribute view custom palette against malformed colors.

Source

Thrown at kernel/av/color.go:152

	}

	ret = &AttributeViewCustomColor{Index: color.Index, Hidden: color.Hidden}
	ret.Light, err = normalizeAttributeViewColorTheme(color.Light)
	if nil != err {
		return nil, fmt.Errorf("invalid light theme of attribute view custom color [%d]: %w", color.Index, err)
	}
	ret.Dark, err = normalizeAttributeViewColorTheme(color.Dark)
	if nil != err {
		return nil, fmt.Errorf("invalid dark theme of attribute view custom color [%d]: %w", color.Index, err)
	}
	return
}

func normalizeAttributeViewColorTheme(theme AttributeViewColorTheme) (ret AttributeViewColorTheme, err error) {
	ret.Color = strings.ToLower(strings.TrimSpace(theme.Color))
	ret.BackgroundColor = strings.ToLower(strings.TrimSpace(theme.BackgroundColor))
	if !attributeViewColorPattern.MatchString(ret.Color) {
		return AttributeViewColorTheme{}, fmt.Errorf("invalid foreground color [%s]", theme.Color)
	}
	if !attributeViewColorPattern.MatchString(ret.BackgroundColor) {
		return AttributeViewColorTheme{}, fmt.Errorf("invalid background color [%s]", theme.BackgroundColor)
	}
	return
}

// WorkspacePalette 返回当前工作空间的数据库自定义色和混排顺序。
func WorkspacePalette() (colors []*AttributeViewCustomColor, order []string) {
	if LoadWorkspacePalette != nil {
		colors, order = LoadWorkspacePalette()
	}
	if nil == colors {
		colors = []*AttributeViewCustomColor{}
	}
	order = NormalizeAttributeViewColorOrder(order, colors)
	return
}

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Correct the foreground color to the exact format the pattern expects (typically full hex like #aabbcc)
  2. Trim whitespace and lowercase the value before passing it in, though trimming is already applied internally — ensure the raw format is right
  3. Pre-validate color strings client-side with the same regex before persisting

Example fix

// before
theme.Color = "#FFF"
// after
theme.Color = "#ffffff"
Defensive patterns

Strategy: validation

Validate before calling

if (!/^#[0-9a-fA-F]{6}$/.test(theme.color)) throw new Error(`invalid foreground color [${theme.color}]`);

Type guard

function isValidForegroundColor(t) { return typeof t?.color === 'string' && /^#[0-9a-f]{6}$/i.test(t.color); }

Prevention

When it happens

Trigger: Any call path into normalizeAttributeViewCustomColor (hence NormalizeAttributeViewCustomColors / resolveColor) where theme.Color is not matched by the color regex — e.g. empty string, named colors, non-hex values.

Common situations: Importing a database or config JSON with colors produced by another tool; typos like 'ff0000' (missing '#') or '#fff' (3-digit shorthand not allowed by the pattern).

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/93228dffc6f4efd3. Report an issue: GitHub.