Skip to content

Commit

Permalink
feat: add hsl and hsla functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bsushmith committed Apr 29, 2024
1 parent 1ce4b47 commit 1280ce3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions styles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
15 changes: 15 additions & 0 deletions styles/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 1280ce3

Please sign in to comment.