render

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package render handles terminal color output for wondertone Tones.

It detects the terminal's color capability and outputs the best possible ANSI escape sequence — TrueColor, 256-color, or 16-color. Downsampling is perceptual (OKLab ΔE nearest-neighbor), not RGB distance.

Import as "render":

import render "github.com/leraniode/wondertone/render"

profile := render.Detect()
fmt.Print(render.FG(myTone, profile), "Hello, wondertone", render.Reset)

Index

Constants

View Source
const (
	Reset     = "\x1b[0m"
	Bold      = "\x1b[1m"
	Dim       = "\x1b[2m"
	Italic    = "\x1b[3m"
	Underline = "\x1b[4m"
)

ANSI escape codes

Variables

This section is empty.

Functions

func BG

func BG(t tone.Tone, p Profile) string

BG returns the ANSI escape sequence to set the background to the given Tone.

func Colorize

func Colorize(t tone.Tone, p Profile, text string) string

Colorize wraps text with FG color and a reset.

func ColorizeOnBG

func ColorizeOnBG(fg, bg tone.Tone, p Profile, text string) string

ColorizeOnBG wraps text with FG and BG colors and a reset.

func FG

func FG(t tone.Tone, p Profile) string

FG returns the ANSI escape sequence to set the foreground to the given Tone. The sequence is adapted to the terminal Profile.

func LipglossColor

func LipglossColor(t tone.Tone, p Profile) string

LipglossColor converts a Tone to a lipgloss-compatible color value string.

Usage with lipgloss (import lipgloss separately):

import "github.com/charmbracelet/lipgloss"

style := lipgloss.NewStyle().
    Foreground(lipgloss.Color(render.LipglossColor(myTone, profile)))

Returns a string that lipgloss.Color() accepts:

  • TrueColor: "#rrggbb"
  • ANSI256: "42" (the 256-color index as a string)
  • ANSI16: "2" (the 16-color index as a string)
  • NoColor: "" (lipgloss will render unstyled)

func LipglossColorHex

func LipglossColorHex(t tone.Tone) string

LipglossColorHex always returns the hex value regardless of profile. Use this when lipgloss itself handles profile detection (e.g. via ColorProfile adapter).

func Swatch

func Swatch(t tone.Tone, p Profile, width int) string

Swatch returns a colored block for palette preview. width is the number of full-block characters (█) to render.

Types

type Profile

type Profile int

Profile represents the terminal's color capability.

const (
	// TrueColor supports 24-bit RGB — the full wondertone experience.
	TrueColor Profile = iota
	// ANSI256 supports 256 colors — perceptual nearest-neighbor downsampling.
	ANSI256
	// ANSI16 supports the 16 standard ANSI colors — best-effort perceptual map.
	ANSI16
	// NoColor disables all color output.
	NoColor
)

func Detect

func Detect() Profile

Detect reads environment variables to determine the terminal's color profile. Checks (in order): NO_COLOR, COLORTERM, TERM, TERM_PROGRAM, CI.

For guaranteed detection in production, pair with charmbracelet/colorprofile.

func Force

func Force(p Profile) Profile

Force returns a Profile that always returns the given level, ignoring env. Useful for testing or when you know the terminal capability.

func (Profile) String

func (p Profile) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL