-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for specifying color transparency with #rgba and #rrggb…
…baa. (#152)
- Loading branch information
Showing
9 changed files
with
214 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"image/color" | ||
) | ||
|
||
func ParseColor(scol string) (color.Color, error) { | ||
var format string | ||
var fourBits bool | ||
var hasAlpha bool | ||
|
||
switch len(scol) { | ||
case 4: | ||
format = "#%1x%1x%1x" | ||
fourBits = true | ||
hasAlpha = false | ||
case 5: | ||
format = "#%1x%1x%1x%1x" | ||
fourBits = true | ||
hasAlpha = true | ||
case 7: | ||
format = "#%02x%02x%02x" | ||
fourBits = false | ||
hasAlpha = false | ||
case 9: | ||
format = "#%02x%02x%02x%02x" | ||
fourBits = false | ||
hasAlpha = true | ||
default: | ||
return color.Gray{0}, fmt.Errorf("color: %v is not a hex-color", scol) | ||
} | ||
|
||
var r, g, b, a uint8 | ||
|
||
if hasAlpha { | ||
n, err := fmt.Sscanf(scol, format, &r, &g, &b, &a) | ||
if err != nil { | ||
return color.Gray{0}, err | ||
} | ||
if n != 4 { | ||
return color.Gray{0}, fmt.Errorf("color: %v is not a hex-color", scol) | ||
} | ||
} else { | ||
n, err := fmt.Sscanf(scol, format, &r, &g, &b) | ||
if err != nil { | ||
return color.Gray{0}, err | ||
} | ||
if n != 3 { | ||
return color.Gray{0}, fmt.Errorf("color: %v is not a hex-color", scol) | ||
} | ||
if fourBits { | ||
a = 15 | ||
} else { | ||
a = 255 | ||
} | ||
} | ||
|
||
if fourBits { | ||
r |= r << 4 | ||
g |= g << 4 | ||
b |= b << 4 | ||
a |= a << 4 | ||
} | ||
|
||
return color.NRGBA{r, g, b, a}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package render | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func ParseAndAssertColor( | ||
t *testing.T, | ||
scol string, | ||
expectedR uint32, | ||
expectedG uint32, | ||
expectedB uint32, | ||
expectedA uint32, | ||
) { | ||
c, err := ParseColor(scol) | ||
assert.Nil(t, err) | ||
r, g, b, a := c.RGBA() | ||
assert.Equal(t, expectedR, r) | ||
assert.Equal(t, expectedG, g) | ||
assert.Equal(t, expectedB, b) | ||
assert.Equal(t, expectedA, a) | ||
} | ||
|
||
func TestParseColorRGB(t *testing.T) { | ||
ParseAndAssertColor(t, "#5ad", 0x5555, 0xaaaa, 0xdddd, 0xffff) | ||
} | ||
|
||
func TestParseColorRGBA(t *testing.T) { | ||
ParseAndAssertColor(t, "#5ad8", 0x2d82, 0x5b05, 0x7653, 0x8888) | ||
} | ||
|
||
func TestParseColorRRGGBB(t *testing.T) { | ||
ParseAndAssertColor(t, "#257adb", 0x2525, 0x7a7a, 0xdbdb, 0xffff) | ||
} | ||
|
||
func TestParseColorRRGGBBAA(t *testing.T) { | ||
ParseAndAssertColor(t, "#257adb75", 0x110a, 0x3831, 0x64df, 0x7575) | ||
} | ||
|
||
func TestParseColorBadValue(t *testing.T) { | ||
_, err := ParseColor("5ad") | ||
assert.NotNil(t, err) | ||
|
||
_, err = ParseColor("#5a") | ||
assert.NotNil(t, err) | ||
|
||
_, err = ParseColor("#5ad8f") | ||
assert.NotNil(t, err) | ||
|
||
_, err = ParseColor("#5ad8f33da") | ||
assert.NotNil(t, err) | ||
|
||
_, err = ParseColor("#xyz") | ||
assert.NotNil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.