Skip to content

Commit

Permalink
Merge pull request #141 from bsushmith/add/hsl_hsla
Browse files Browse the repository at this point in the history
feat: add hsl and hsla functions
  • Loading branch information
chasefleming committed May 3, 2024
2 parents 1ce4b47 + 0984645 commit 3c09cdd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions styles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ This function returns a string representation of the given RGBA color.
rgbaColor := styles.RGBA(255, 0, 0, 0.5) // Returns "rgba(255, 0, 0, 0.5)"
```

##### `HSL(h, s, l int) string`

This function returns a string representation of the given HSL color.

```go
hslColor := styles.HSL(120, 100, 50) // Returns "hsl(120, 100%, 50%)"
```

##### `HSLA(h, s, l int, a float64) string`

This function returns a string representation of the given HSLA color.

```go
hslaColor := styles.HSLA(120, 100, 50, 0.5) // Returns "hsla(120, 100%, 50%, 0.5)"
```


#### Other Functions

##### `Int(value int) string`
Expand Down
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(saturation), 10))
builder.WriteString("%,")
builder.WriteString(strconv.FormatInt(int64(lightness), 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(saturation), 10))
builder.WriteString("%,")
builder.WriteString(strconv.FormatInt(int64(lightness), 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,50%,25%)", HSL(100, 50, 25))
assert.Equal(t, "hsl(0,0%,0%)", HSL(0, 0, 0))
assert.Equal(t, "hsl(50,70%,80%)", HSL(50, 70, 80))
assert.Equal(t, "hsl(20,40%,60%)", HSL(20, 40, 60))
}

func TestHSLA(t *testing.T) {
assert.Equal(t, "hsla(90,50%,30%,1.00)", HSLA(90, 50, 30, 1))
assert.Equal(t, "hsla(0,0%,0%,0.10)", HSLA(0, 0, 0, 0.1))
assert.Equal(t, "hsla(50,60%,70%,0.50)", HSLA(50, 60, 70, 0.5000))
assert.Equal(t, "hsla(25,35%,45%,0.25)", HSLA(25, 35, 45, 0.2500))
assert.Equal(t, "hsla(20,50%,80%,0.00)", HSLA(20, 50, 80, 0))
}

0 comments on commit 3c09cdd

Please sign in to comment.