package ui

import (
	
	
	
)

// Color represents a color.
type Color interface {
	fgSGR() string
	bgSGR() string
	String() string
}

// Builtin ANSI colors.
var (
	Black   Color = ansiColor(0)
	Red     Color = ansiColor(1)
	Green   Color = ansiColor(2)
	Yellow  Color = ansiColor(3)
	Blue    Color = ansiColor(4)
	Magenta Color = ansiColor(5)
	Cyan    Color = ansiColor(6)
	White   Color = ansiColor(7)

	BrightBlack   Color = ansiBrightColor(0)
	BrightRed     Color = ansiBrightColor(1)
	BrightGreen   Color = ansiBrightColor(2)
	BrightYellow  Color = ansiBrightColor(3)
	BrightBlue    Color = ansiBrightColor(4)
	BrightMagenta Color = ansiBrightColor(5)
	BrightCyan    Color = ansiBrightColor(6)
	BrightWhite   Color = ansiBrightColor(7)
)

// XTerm256Color returns a color from the xterm 256-color palette.
func ( uint8) Color { return xterm256Color() }

// TrueColor returns a 24-bit true color.
func (, ,  uint8) Color { return trueColor{, , } }

var colorNames = []string{
	"black", "red", "green", "yellow",
	"blue", "magenta", "cyan", "white",
}

var colorByName = map[string]Color{
	"black":   Black,
	"red":     Red,
	"green":   Green,
	"yellow":  Yellow,
	"blue":    Blue,
	"magenta": Magenta,
	"cyan":    Cyan,
	"white":   White,

	"bright-black":   BrightBlack,
	"bright-red":     BrightRed,
	"bright-green":   BrightGreen,
	"bright-yellow":  BrightYellow,
	"bright-blue":    BrightBlue,
	"bright-magenta": BrightMagenta,
	"bright-cyan":    BrightCyan,
	"bright-white":   BrightWhite,
}

type ansiColor uint8

func ( ansiColor) () string  { return strconv.Itoa(30 + int()) }
func ( ansiColor) () string  { return strconv.Itoa(40 + int()) }
func ( ansiColor) () string { return colorNames[] }

type ansiBrightColor uint8

func ( ansiBrightColor) () string  { return strconv.Itoa(90 + int()) }
func ( ansiBrightColor) () string  { return strconv.Itoa(100 + int()) }
func ( ansiBrightColor) () string { return "bright-" + colorNames[] }

type xterm256Color uint8

func ( xterm256Color) () string  { return "38;5;" + strconv.Itoa(int()) }
func ( xterm256Color) () string  { return "48;5;" + strconv.Itoa(int()) }
func ( xterm256Color) () string { return "color" + strconv.Itoa(int()) }

type trueColor struct{ r, g, b uint8 }

func ( trueColor) () string { return "38;2;" + .rgbSGR() }
func ( trueColor) () string { return "48;2;" + .rgbSGR() }

func ( trueColor) () string {
	return fmt.Sprintf("#%02x%02x%02x", .r, .g, .b)
}

func ( trueColor) () string {
	return fmt.Sprintf("%d;%d;%d", .r, .g, .b)
}

func ( string) Color {
	if ,  := colorByName[];  {
		return 
	}
	if strings.HasPrefix(, "color") {
		,  := strconv.Atoi([5:])
		if  == nil && 0 <=  &&  < 256 {
			return XTerm256Color(uint8())
		}
	} else if strings.HasPrefix(, "#") && len() == 7 {
		,  := strconv.ParseUint([1:3], 16, 8)
		,  := strconv.ParseUint([3:5], 16, 8)
		,  := strconv.ParseUint([5:7], 16, 8)
		if  == nil &&  == nil &&  == nil {
			return TrueColor(uint8(), uint8(), uint8())
		}
	}
	return nil
}