package lipgloss

import (
	
	
	
	
	

	
	
	
)

func [ cmp.Ordered](, ,  )  {
	if  <  {
		,  = , 
	}
	return min(, max(, ))
}

// 4-bit color constants.
const (
	Black ansi.BasicColor = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White

	BrightBlack
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	BrightWhite
)

var noColor = NoColor{}

// NoColor is used to specify the absence of color styling. When this is active
// foreground colors will be rendered with the terminal's default text color,
// and background colors will not be drawn at all.
//
// Example usage:
//
//	var style = someStyle.Background(lipgloss.NoColor{})
type NoColor struct{}

// RGBA returns the RGBA value of this color. Because we have to return
// something, despite this color being the absence of color, we're returning
// black with 100% opacity.
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
func ( NoColor) () (, , ,  uint32) {
	return 0x0, 0x0, 0x0, 0xFFFF //nolint:mnd
}

// Color specifies a color by hex or ANSI256 value. For example:
//
//	ansiColor := lipgloss.Color("1") // The same as lipgloss.Red
//	ansi256Color := lipgloss.Color("21")
//	hexColor := lipgloss.Color("#0000ff")
func ( string) color.Color {
	if strings.HasPrefix(, "#") {
		,  := parseHex()
		if  != nil {
			return noColor
		}
		return 
	}

	,  := strconv.Atoi()
	if  != nil {
		return noColor
	}

	if  < 0 {
		// Only positive numbers
		 = -
	}

	if  < 16 {
		return ansi.BasicColor() //nolint:gosec
	} else if  < 256 {
		return ANSIColor() //nolint:gosec
	}

	, ,  := uint8((>>16)&0xff), uint8(>>8&0xff), uint8(&0xff) //nolint:gosec
	return color.RGBA{R: , G: , B: , A: 0xff}
}

var errInvalidFormat = errors.New("invalid hex format") // pre-allocated.

// parseHex parses a hex color string and returns a color.RGBA. The string can be
// in the format #RRGGBB or #RGB. This is a more performant implementation of
// [colorful.Hex].
func ( string) ( color.RGBA,  error) {
	.A = 0xff

	if len() == 0 || [0] != '#' {
		return , errInvalidFormat
	}

	 := func( byte) byte {
		switch {
		case  >= '0' &&  <= '9':
			return  - '0'
		case  >= 'a' &&  <= 'f':
			return  - 'a' + 10
		case  >= 'A' &&  <= 'F':
			return  - 'A' + 10
		}
		 = errInvalidFormat
		return 0
	}

	switch len() {
	case 7:
		.R = ([1])<<4 + ([2])
		.G = ([3])<<4 + ([4])
		.B = ([5])<<4 + ([6])
	case 4:
		.R = ([1]) * 17
		.G = ([2]) * 17
		.B = ([3]) * 17
	default:
		 = errInvalidFormat
	}
	return , 
}

// RGBColor is a color specified by red, green, and blue values.
type RGBColor struct {
	R uint8
	G uint8
	B uint8
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface.
func ( RGBColor) () (, , ,  uint32) {
	return color.RGBA{R: .R, G: .G, B: .B, A: 255}.RGBA()
}

// ANSIColor is a color specified by an ANSI256 color value.
//
// Example usage:
//
//	colorA := lipgloss.ANSIColor(8)
//	colorB := lipgloss.ANSIColor(134)
type ANSIColor = ansi.IndexedColor

// LightDarkFunc is a function that returns a color based on whether the
// terminal has a light or dark background. You can create one of these with
// [LightDark].
//
// Example:
//
//	lightDark := lipgloss.LightDark(hasDarkBackground)
//	red, blue := lipgloss.Color("#ff0000"), lipgloss.Color("#0000ff")
//	myHotColor := lightDark(red, blue)
//
// For more info see [LightDark].
type LightDarkFunc func(light, dark color.Color) color.Color

// LightDark is a simple helper type that can be used to choose the appropriate
// color based on whether the terminal has a light or dark background.
//
//	lightDark := lipgloss.LightDark(hasDarkBackground)
//	red, blue := lipgloss.Color("#ff0000"), lipgloss.Color("#0000ff")
//	myHotColor := lightDark(red, blue)
//
// In practice, there are slightly different workflows between Bubble Tea and
// Lip Gloss standalone.
//
// In Bubble Tea, listen for tea.BackgroundColorMsg, which automatically
// flows through Update on start. This message will be received whenever the
// background color changes:
//
//	case tea.BackgroundColorMsg:
//	    m.hasDarkBackground = msg.IsDark()
//
// Later, when you're rendering use:
//
//	lightDark := lipgloss.LightDark(m.hasDarkBackground)
//	red, blue := lipgloss.Color("#ff0000"), lipgloss.Color("#0000ff")
//	myHotColor := lightDark(red, blue)
//
// In standalone Lip Gloss, the workflow is simpler:
//
//	hasDarkBG := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
//	lightDark := lipgloss.LightDark(hasDarkBG)
//	red, blue := lipgloss.Color("#ff0000"), lipgloss.Color("#0000ff")
//	myHotColor := lightDark(red, blue)
func ( bool) LightDarkFunc {
	return func(,  color.Color) color.Color {
		if  {
			return 
		}
		return 
	}
}

// isDarkColor returns whether the given color is dark, based on its CIELAB
// lightness.
//
// Example usage:
//
//	color := lipgloss.Color("#0000ff")
//	if lipgloss.isDarkColor(color) {
//		fmt.Println("It's dark! I love darkness!")
//	} else {
//		fmt.Println("It's light! Cover your eyes!")
//	}
func ( color.Color) bool {
	,  := colorful.MakeColor()
	if ! {
		return true
	}

	, ,  := .Lab()
	return  < 0.5 //nolint:mnd
}

// CompleteFunc is a function that returns the appropriate color based on the
// given color profile.
//
// Example usage:
//
//	p := colorprofile.Detect(os.Stderr, os.Environ())
//	complete := lipgloss.Complete(p)
//	color := complete(
//		lipgloss.Color(1), // ANSI
//		lipgloss.Color(124), // ANSI256
//		lipgloss.Color("#ff34ac"), // TrueColor
//	)
//	fmt.Println("Ooh, pretty color: ", color)
//
// For more info see [Complete].
type CompleteFunc func(ansi, ansi256, truecolor color.Color) color.Color

// Complete returns a function that will return the appropriate color based on
// the given color profile.
//
// Example usage:
//
//	p := colorprofile.Detect(os.Stderr, os.Environ())
//	complete := lipgloss.Complete(p)
//	color := complete(
//	    lipgloss.Color(1), // ANSI
//	    lipgloss.Color(124), // ANSI256
//	    lipgloss.Color("#ff34ac"), // TrueColor
//	)
//	fmt.Println("Ooh, pretty color: ", color)
func ( colorprofile.Profile) CompleteFunc {
	return func(, ,  color.Color) color.Color {
		switch  { //nolint:exhaustive
		case colorprofile.ANSI:
			return 
		case colorprofile.ANSI256:
			return 
		case colorprofile.TrueColor:
			return 
		}
		return noColor
	}
}

// ensureNotTransparent ensures that the alpha value of a color is not 0, and if
// it is, we will set it to 1. This is useful for when we are converting from
// RGB -> RGBA, and the alpha value is lost in the conversion for gradient purposes.
func ( color.Color) color.Color {
	, , ,  := .RGBA()
	if  == 0 {
		return Alpha(, 1)
	}
	return 
}

// Alpha adjusts the alpha value of a color using a 0-1 (clamped) float scale
// 0 = transparent, 1 = opaque.
func ( color.Color,  float64) color.Color {
	if  == nil {
		return nil
	}

	, , ,  := .RGBA()
	return color.RGBA{
		R: uint8(min(255, float64(>>8))),
		G: uint8(min(255, float64(>>8))),
		B: uint8(min(255, float64(>>8))),
		A: uint8(clamp(, 0, 1) * 255),
	}
}

// Complementary returns the complementary color (180° away on color wheel) of
// the given color. This is useful for creating a contrasting color.
func ( color.Color) color.Color {
	if  == nil {
		return nil
	}

	// Offset hue by 180°.
	,  := colorful.MakeColor(ensureNotTransparent())

	, ,  := .Hsv()
	 += 180
	if  >= 360 {
		 -= 360
	} else if  < 0 {
		 += 360
	}

	return colorful.Hsv(, , ).Clamped()
}

// Darken takes a color and makes it darker by a specific percentage (0-1, clamped).
func ( color.Color,  float64) color.Color {
	if  == nil {
		return nil
	}

	 := 1.0 - clamp(, 0, 1)

	, , ,  := .RGBA()
	return color.RGBA{
		R: uint8(float64(>>8) * ),
		G: uint8(float64(>>8) * ),
		B: uint8(float64(>>8) * ),
		A: uint8(min(255, float64(>>8))),
	}
}

// Lighten makes a color lighter by a specific percentage (0-1, clamped).
func ( color.Color,  float64) color.Color {
	if  == nil {
		return nil
	}

	 := 255 * (clamp(, 0, 1))

	, , ,  := .RGBA()
	return color.RGBA{
		R: uint8(min(255, float64(>>8)+)),
		G: uint8(min(255, float64(>>8)+)),
		B: uint8(min(255, float64(>>8)+)),
		A: uint8(min(255, float64(>>8))),
	}
}