yorukot/superfile · error

invalid background color: %w

Error message

invalid background color: %w

What it means

ANSIRenderer converts an image to an ANSI art string, first parsing the supplied defaultBGColor via hexToColor. This error wraps hexToColor's failure, meaning the background color string is not a valid hex color (e.g., "#fff", "fff", or a named color). The rendering cannot proceed without a valid background color.

Source

Thrown at src/pkg/file_preview/ansi.go:49

			// Using the "▄" character which fills the lower half
			cell := termenv.String("▄").Foreground(lowerColor).Background(upperColor)
			output.WriteString(cell.String())
		}
		// Only add newline if this is not the last row
		if y+2 < height {
			output.WriteByte('\n')
		}
	}

	return output.String()
}

// Convert image to ansi
func (p *ImagePreviewer) ANSIRenderer(img image.Image, defaultBGColor string,
	maxWidth int, maxHeight int) (string, error) {
	bgColor, err := hexToColor(defaultBGColor)
	if err != nil {
		return "", fmt.Errorf("invalid background color: %w", err)
	}

	// For ANSI rendering, resize image appropriately
	fittedImg := resizeForANSI(img, maxWidth, maxHeight)
	return ConvertImageToANSI(fittedImg, bgColor), nil
}

type colorCache struct {
	rgbaToTermenv map[color.RGBA]termenv.RGBColor
}

func newColorCache() *colorCache {
	return &colorCache{
		rgbaToTermenv: make(map[color.RGBA]termenv.RGBColor),
	}
}

func (c *colorCache) getTermenvColor(col color.Color, fallbackColor string) termenv.RGBColor {

View on GitHub (pinned to b72f550bc6)

Solutions

  1. Pass a valid 6-digit hex color like "#000000" as defaultBGColor
  2. Validate/sanitize the color from user config before calling, falling back to a default on parse failure
  3. Accept 3-digit hex only if hexToColor supports it; otherwise expand "#fff" to "#ffffff"
  4. Check the wrapped error to confirm it is a parse failure, not a config-loading problem

Example fix

// before
previewer.ANSIRenderer(img, cfg.BGColor, w, h) // cfg.BGColor = "black"
// after
bg := cfg.BGColor
if _, err := hexToColor(bg); err != nil {
	bg = "#000000" // safe default
}
previewer.ANSIRenderer(img, bg, w, h)
Defensive patterns

Strategy: validation

Validate before calling

func isValidHexColor(s string) bool {
	_, err := hexToColor(s)
	return err == nil
}

Try / catch

render, err := previewer.ANSIRenderer(img, bg, w, h)
if err != nil && strings.Contains(err.Error(), "invalid background color") {
	render, err = previewer.ANSIRenderer(img, "#000000", w, h)
}

Prevention

When it happens

Trigger: Calling ImagePreviewWithRenderer (which calls ANSIRenderer) with a defaultBGColor that is not parseable as a hex color — wrong format, empty string, missing '#', or unsupported bit depth.

Common situations: User config file contains an invalid bg color value; theme passes a named color like "black" instead of "#000000"; empty string from an unset config key.

Related errors


AI-assisted analysis of yorukot/superfile@b72f550bc6 (2026-09-01). Data as JSON: /api/errors/dd9af55263d6f2ef. Report an issue: GitHub.