package csscolorparser

import (
	
	
)

// R, G, B, A values in the range 0..1
type Color struct {
	R, G, B, A float64
}

// Implement the Go color.Color interface.
func ( Color) () (, , ,  uint32) {
	 := clamp(.A)
	 = uint32(clamp(.R)**65535 + 0.5)
	 = uint32(clamp(.G)**65535 + 0.5)
	 = uint32(clamp(.B)**65535 + 0.5)
	 = uint32(*65535 + 0.5)
	return
}

// RGBA255 returns R, G, B, A values in the range 0..255
func ( Color) () (, , ,  uint8) {
	 = uint8(clamp(.R)*255 + 0.5)
	 = uint8(clamp(.G)*255 + 0.5)
	 = uint8(clamp(.B)*255 + 0.5)
	 = uint8(clamp(.A)*255 + 0.5)
	return
}

// Clamp restricts R, G, B, A values to the range 0..1.
func ( Color) () Color {
	return Color{
		R: math.Max(math.Min(.R, 1), 0),
		G: math.Max(math.Min(.G, 1), 0),
		B: math.Max(math.Min(.B, 1), 0),
		A: math.Max(math.Min(.A, 1), 0),
	}
}

// HexString returns CSS hexadecimal string.
func ( Color) () string {
	, , ,  := .RGBA255()
	if  < 255 {
		return fmt.Sprintf("#%02x%02x%02x%02x", , , , )
	}
	return fmt.Sprintf("#%02x%02x%02x", , , )
}

// RGBString returns CSS RGB string.
func ( Color) () string {
	, , ,  := .RGBA255()
	 := uint8(clamp(.A) * 100)
	if  < 100 {
		return fmt.Sprintf("rgb(%d %d %d / %d%%)", , , , )
	}
	return fmt.Sprintf("rgb(%d %d %d)", , , )
}

// Name returns name of this color if its available.
func ( Color) () (string, bool) {
	, , ,  := .RGBA255()
	 := [3]uint8{, , }
	for ,  := range NamedColors {
		if  ==  {
			return , true
		}
	}
	return "", false
}

// Implement the Go TextUnmarshaler interface
func ( *Color) ( []byte) error {
	,  := Parse(string())
	if  != nil {
		return 
	}
	.R = .R
	.G = .G
	.B = .B
	.A = .A
	return nil
}

// Implement the Go TextMarshaler interface
func ( Color) () ([]byte, error) {
	return []byte(.HexString()), nil
}

// FromHsv creates a Color from HSV colors.
//
// Arguments:
//
//   - h: Hue angle [0..360]
//   - s: Saturation [0..1]
//   - v: Value [0..1]
//   - a: Alpha [0..1]
func (, , ,  float64) Color {
	, ,  := hsvToRgb(normalizeAngle(), clamp(), clamp())
	return Color{, , , clamp()}
}

// FromHsl creates a Color from HSL colors.
//
// Arguments:
//
//   - h: Hue angle [0..360]
//   - s: Saturation [0..1]
//   - l: Lightness [0..1]
//   - a: Alpha [0..1]
func (, , ,  float64) Color {
	, ,  := hslToRgb(normalizeAngle(), clamp(), clamp())
	return Color{, , , clamp()}
}

// FromHwb creates a Color from HWB colors.
//
// Arguments:
//
//   - h: Hue angle [0..360]
//   - w: Whiteness [0..1]
//   - b: Blackness [0..1]
//   - a: Alpha [0..1]
func (, , ,  float64) Color {
	, ,  := hwbToRgb(normalizeAngle(), clamp(), clamp())
	return Color{, , , clamp()}
}

func fromLinear( float64) float64 {
	if  >= 0.0031308 {
		return 1.055*math.Pow(, 1.0/2.4) - 0.055
	}
	return 12.92 * 
}

// FromLinearRGB creates a Color from linear-light RGB colors.
//
// Arguments:
//
//   - r: Red value [0..1]
//   - g: Green value [0..1]
//   - b: Blue value [0..1]
//   - a: Alpha value [0..1]
func (, , ,  float64) Color {
	return Color{fromLinear(), fromLinear(), fromLinear(), clamp()}
}

// FromOklab creates a Color from Oklab colors.
//
// Arguments:
//
//   - l: Perceived lightness
//   - a: How green/red the color is
//   - b: How blue/yellow the color is
//   - alpha: Alpha [0..1]
func (, , ,  float64) Color {
	 := math.Pow(+0.3963377774*+0.2158037573*, 3)
	 := math.Pow(-0.1055613458*-0.0638541728*, 3)
	 := math.Pow(-0.0894841775*-1.2914855480*, 3)

	 := 4.0767416621* - 3.3077115913* + 0.2309699292*
	 := -1.2684380046* + 2.6097574011* - 0.3413193965*
	 := -0.0041960863* - 0.7034186147* + 1.7076147010*

	return FromLinearRGB(, , , )
}

// FromOklch creates a Color from OKLCh colors.
//
// Arguments:
//
//   - l: Perceived lightness
//   - c: Chroma
//   - h: Hue angle in radians
//   - alpha: Alpha [0..1]
func (, , ,  float64) Color {
	return FromOklab(, *math.Cos(), *math.Sin(), )
}

func (, , ,  float64) Color {
	, ,  := labToLinearRGB(, , )
	return FromLinearRGB(, , , )
}

func (, , ,  float64) Color {
	if math.IsNaN() {
		 = 0
	}
	return FromLab(, *math.Cos(), *math.Sin(), )
}

func (, , ,  float64) Color {
	, ,  := xyzD50ToLinearRGB(, , )
	return FromLinearRGB(, , , )
}

func (, , ,  float64) Color {
	, ,  := xyzD65ToLinearRgb(, , )
	return FromLinearRGB(, , , )
}

func ( Color) () [4]float64 {
	 = .Clamp()
	, ,  := rgbToHsl(.R, .G, .B)
	return [4]float64{, , , .A}
}

func ( Color) () [4]float64 {
	 = .Clamp()
	, ,  := rgbToHwb(.R, .G, .B)
	return [4]float64{, , , .A}
}

func ( Color) () [4]float64 {
	 := func( float64) float64 {
		if  >= 0.04045 {
			return math.Pow((+0.055)/1.055, 2.4)
		}
		return  / 12.92
	}
	return [4]float64{
		(.R),
		(.G),
		(.B),
		.A,
	}
}

func ( Color) () [4]float64 {
	 := .LinearRgb()
	, ,  := linearRGBToLab([0], [1], [2])
	return [4]float64{, , , .A}
}

func ( Color) () [4]float64 {
	 := .Lab()
	 := [0]
	 := [1]
	 := [2]
	 := math.Hypot(, )
	 := math.Atan2(, )
	if  < 0 {
		 += math.Pi * 2
	}
	return [4]float64{, , , .A}
}

func ( Color) () [4]float64 {
	 := .LinearRgb()
	 := math.Cbrt(0.4121656120*[0] + 0.5362752080*[1] + 0.0514575653*[2])
	 := math.Cbrt(0.2118591070*[0] + 0.6807189584*[1] + 0.1074065790*[2])
	 := math.Cbrt(0.0883097947*[0] + 0.2818474174*[1] + 0.6302613616*[2])
	return [4]float64{
		0.2104542553* + 0.7936177850* - 0.0040720468*,
		1.9779984951* - 2.4285922050* + 0.4505937099*,
		0.0259040371* + 0.7827717662* - 0.8086757660*,
		.A,
	}
}

func ( Color) () [4]float64 {
	 := .Oklab()
	 := [1]
	 := [2]
	 := math.Sqrt(* + *)
	 := math.Atan2(, )
	return [4]float64{[0], , , [3]}
}

func ( Color) () [4]float64 {
	 := .LinearRgb()
	, ,  := linearRGBToXYZD50([0], [1], [2])
	return [4]float64{, , , .A}
}

func ( Color) () [4]float64 {
	 := .LinearRgb()
	, ,  := linearRgbToXyzD65([0], [1], [2])
	return [4]float64{, , , .A}
}

// Utils

func hueToRgb(, ,  float64) float64 {
	 = modulo(, 6)
	if  < 1 {
		return  + (( - ) * )
	}
	if  < 3 {
		return 
	}
	if  < 4 {
		return  + (( - ) * (4 - ))
	}
	return 
}

// h = 0..360
// s, l = 0..1
// r, g, b = 0..1
func hslToRgb(, ,  float64) (, ,  float64) {
	if  == 0 {
		return , , 
	}
	var  float64
	if  < 0.5 {
		 =  * (1 + )
	} else {
		 =  +  - ( * )
	}
	 := 2* - 
	 /= 60
	 = clamp(hueToRgb(, , +2))
	 = clamp(hueToRgb(, , ))
	 = clamp(hueToRgb(, , -2))
	return
}

func hwbToRgb(, ,  float64) (, ,  float64) {
	if + >= 1 {
		 :=  / ( + )
		return , , 
	}
	, ,  = hslToRgb(, 1, 0.5)
	 = *(1--) + 
	 = *(1--) + 
	 = *(1--) + 
	return
}

func hsvToHsl(, ,  float64) (, ,  float64) {
	 = 
	 = 
	 = (2 - ) *  / 2
	if  != 0 {
		if  == 1 {
			 = 0
		} else if  < 0.5 {
			 =  *  / ( * 2)
		} else {
			 =  *  / (2 - *2)
		}
	}
	return
}

func hsvToRgb(, ,  float64) (, ,  float64) {
	, ,  := hsvToHsl(, , )
	return hslToRgb(, , )
}

// ---

// hue returns the hue in degrees [0, 360) for sRGB values in [0, 1],
// given the precomputed max and min channel values.
// Achromatic colors (max == min) return 0.
func hue(, , , ,  float64) float64 {
	 :=  - 
	if  == 0 {
		return 0
	}

	var  float64
	switch  {
	case :
		 = math.Mod((-)/, 6)
	case :
		 = (-)/ + 2
	default:
		 = (-)/ + 4
	}

	 *= 60
	if  < 0 {
		 += 360
	}
	return 
}

// rgbToHsl converts sRGB (r, g, b in [0, 1]) to HSL.
// h is in degrees [0, 360); s and l are in [0, 1].
func rgbToHsl(, ,  float64) (, ,  float64) {
	 := math.Max(, math.Max(, ))
	 := math.Min(, math.Min(, ))
	 :=  - 

	 = ( + ) / 2
	if  == 0 {
		return 0, 0, 
	}

	 =  / (1 - math.Abs(2*-1))
	 = hue(, , , , )
	return , , 
}

// rgbToHwb converts sRGB (r, g, b in [0, 1]) to HWB.
// h is in degrees [0, 360); w (whiteness) and bl (blackness) are in [0, 1].
func rgbToHwb(, ,  float64) (, ,  float64) {
	 := math.Max(, math.Max(, ))
	 := math.Min(, math.Min(, ))

	 = hue(, , , , )
	 = 
	 = 1 - 
	return , , 
}

// converts linear sRGB (Rec. 709 primaries, D65 white point)
// to CIE XYZ (D65). Inputs are in [0, 1]; white (1,1,1) maps to Y = 1.
func linearRgbToXyzD65(, ,  float64) (, ,  float64) {
	 = 0.4123907992659595* + 0.357584339383878* + 0.1804807884018343*
	 = 0.21263900587151036* + 0.715168678767756* + 0.07219231536073371*
	 = 0.01933081871559185* + 0.11919477979462598* + 0.9505321522496606*
	return
}

// converts CIE XYZ (D65) to linear sRGB.
// Results are not clamped, so out-of-gamut colors can fall outside [0, 1].
func xyzD65ToLinearRgb(, ,  float64) (, ,  float64) {
	 = 3.2409699419045226* - 1.537383177570094* - 0.4986107602930034*
	 = -0.9692436362808796* + 1.8759675015077202* + 0.04155505740717559*
	 = 0.05563007969699366* - 0.20397695888897652* + 1.0569715142428786*
	return
}