package csscolorparser

import (
	
	
	
)

func modulo(,  float64) float64 {
	return math.Mod(math.Mod(, )+, )
}

func clamp( float64) float64 {
	if  < 0 {
		return 0
	}
	if  > 1 {
		return 1
	}
	return 
}

func parseFloat( string) (float64, bool) {
	,  := strconv.ParseFloat(strings.TrimSpace(), 64)
	return ,  == nil
}

// Returns (result, ok?, percentage?)
func parsePercentOrFloat( string) (float64, bool, bool) {
	if  == "none" {
		return 0, true, false
	}
	if strings.HasSuffix(, "%") {
		,  := parseFloat([:len()-1])
		if  {
			return  / 100, true, true
		}
		return 0, false, true
	}
	,  := parseFloat()
	return , , false
}

// Result angle in degrees (not normalized)
func parseAngle( string) (float64, bool) {
	if  == "none" {
		return 0, true
	}
	if strings.HasSuffix(, "deg") {
		return parseFloat([:len()-3])
	}
	if strings.HasSuffix(, "grad") {
		,  := parseFloat([:len()-4])
		if  {
			return  / 400 * 360, true
		}
		return 0, false
	}
	if strings.HasSuffix(, "rad") {
		,  := parseFloat([:len()-3])
		if  {
			return  / math.Pi * 180, true
		}
		return 0, false
	}
	if strings.HasSuffix(, "turn") {
		,  := parseFloat([:len()-4])
		if  {
			return  * 360, true
		}
		return 0, false
	}
	return parseFloat()
}

func normalizeAngle( float64) float64 {
	 = math.Mod(, 360)
	if  < 0 {
		 += 360
	}
	return 
}

// Map t which is in range [a, b] to range [c, d]
func remap(, , , ,  float64) float64 {
	return (-)*((-)/(-)) + 
}