package ansi

import (
	
	
	
	

	
)

// colorToHexString returns a hex string representation of a color.
func ( color.Color) string { //nolint:unused
	if  == nil {
		return ""
	}
	 := func( uint32) uint32 {
		if  > 0xff {
			return  >> 8
		}
		return 
	}
	, , ,  := .RGBA()
	, ,  = (), (), ()
	return fmt.Sprintf("#%02x%02x%02x", , , )
}

// rgbToHex converts red, green, and blue values to a hexadecimal value.
//
//	hex := rgbToHex(0, 0, 255) // 0x0000FF
func (, ,  uint32) uint32 { //nolint:unused
	return <<16 + <<8 + 
}

type shiftable interface {
	~uint | ~uint16 | ~uint32 | ~uint64
}

func [ shiftable]( )  {
	if  > 0xff {
		 >>= 8
	}
	return 
}

// XParseColor is a helper function that parses a string into a color.Color. It
// provides a similar interface to the XParseColor function in Xlib. It
// supports the following formats:
//
//   - #RGB
//   - #RRGGBB
//   - rgb:RRRR/GGGG/BBBB
//   - rgba:RRRR/GGGG/BBBB/AAAA
//
// If the string is not a valid color, nil is returned.
//
// See: https://linux.die.net/man/3/xparsecolor
func ( string) color.Color {
	switch {
	case strings.HasPrefix(, "#"):
		,  := colorful.Hex()
		if  != nil {
			return nil
		}

		return 
	case strings.HasPrefix(, "rgb:"):
		 := strings.Split([4:], "/")
		if len() != 3 {
			return nil
		}

		,  := strconv.ParseUint([0], 16, 32)
		,  := strconv.ParseUint([1], 16, 32)
		,  := strconv.ParseUint([2], 16, 32)

		return color.RGBA{uint8(shift()), uint8(shift()), uint8(shift()), 255} //nolint:gosec
	case strings.HasPrefix(, "rgba:"):
		 := strings.Split([5:], "/")
		if len() != 4 {
			return nil
		}

		,  := strconv.ParseUint([0], 16, 32)
		,  := strconv.ParseUint([1], 16, 32)
		,  := strconv.ParseUint([2], 16, 32)
		,  := strconv.ParseUint([3], 16, 32)

		return color.RGBA{uint8(shift()), uint8(shift()), uint8(shift()), uint8(shift())} //nolint:gosec
	}
	return nil
}