diff --git a/styles/utils.go b/styles/utils.go index 5aeaad7..82b9e8a 100644 --- a/styles/utils.go +++ b/styles/utils.go @@ -128,3 +128,30 @@ func Seconds(value float64) string { func Milliseconds(value int) string { return strconv.Itoa(value) + "ms" } + +func HSL(hue, saturation, lightness int) string { + var builder strings.Builder + builder.WriteString("hsl(") + builder.WriteString(strconv.FormatInt(int64(hue), 10)) + builder.WriteString(",") + builder.WriteString(strconv.FormatInt(int64(hue), 10)) + builder.WriteString("%,") + builder.WriteString(strconv.FormatInt(int64(hue), 10)) + builder.WriteString("%") + builder.WriteString(")") + return builder.String() +} + +func HSLA(hue, saturation, lightness int, alpha float64) string { + var builder strings.Builder + builder.WriteString("hsla(") + builder.WriteString(strconv.FormatInt(int64(hue), 10)) + builder.WriteString(",") + builder.WriteString(strconv.FormatInt(int64(hue), 10)) + builder.WriteString("%,") + builder.WriteString(strconv.FormatInt(int64(hue), 10)) + builder.WriteString("%,") + builder.WriteString(strconv.FormatFloat(alpha, 'f', 2, 64)) + builder.WriteString(")") + return builder.String() +} diff --git a/styles/utils_test.go b/styles/utils_test.go index b0e3bfc..081348a 100644 --- a/styles/utils_test.go +++ b/styles/utils_test.go @@ -166,3 +166,18 @@ func TestMilliseconds(t *testing.T) { assert.Equal(t, "0ms", Milliseconds(0)) assert.Equal(t, "110ms", Milliseconds(110)) } + +func TestHSL(t *testing.T) { + assert.Equal(t, "hsl(100,100%,100%)", HSL(100, 100, 100)) + assert.Equal(t, "hsl(0,0%,0%)", HSL(0, 0, 0)) + assert.Equal(t, "hsl(50,50%,50%)", HSL(50, 50, 50)) + assert.Equal(t, "hsl(25,25%,25%)", HSL(25, 25, 25)) +} + +func TestHSLA(t *testing.T) { + assert.Equal(t, "hsla(100,100%,100%,1.00)", HSLA(100, 100, 100, 1)) + assert.Equal(t, "hsla(0,0%,0%,0.10)", HSLA(0, 0, 0, 0.1)) + assert.Equal(t, "hsla(50,50%,50%,0.50)", HSLA(50, 50, 50, 0.5000)) + assert.Equal(t, "hsla(25,25%,25%,0.25)", HSLA(25, 25, 25, 0.2500)) + assert.Equal(t, "hsla(25,25%,25%,0.00)", HSLA(25, 25, 25, 0)) +}