-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
71 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
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,54 @@ | ||
package lights | ||
|
||
type InvalidColorError struct { | ||
color string | ||
msg string | ||
} | ||
|
||
func (err *InvalidColorError) Error() string { | ||
return err.msg | ||
} | ||
|
||
type InvalidColorValue struct { | ||
value uint8 | ||
msg string | ||
} | ||
|
||
func (err *InvalidColorValue) Error() string { | ||
return err.msg | ||
} | ||
|
||
// Color represents a RGBW color | ||
type Color struct { | ||
R uint8 | ||
G uint8 | ||
B uint8 | ||
W uint8 | ||
} | ||
|
||
// NewColor initializes a new Color using valid RGBW values (between 0 - 255). | ||
func NewColor(r uint8, g uint8, b uint8, w uint8) (Color, error) { | ||
var color Color | ||
|
||
if r > 255 { | ||
return color, &InvalidColorValue{r, "Red value is not within range. e.g. Value must be between 0 - 255"} | ||
} | ||
if g > 255 { | ||
return color, &InvalidColorValue{r, "Green value is not within range. e.g. Value must be between 0 - 255"} | ||
} | ||
if b > 255 { | ||
return color, &InvalidColorValue{r, "Blue value is not within range. e.g. Value must be between 0 - 255"} | ||
} | ||
if w > 255 { | ||
return color, &InvalidColorValue{r, "White value is not within range. e.g. Value must be between 0 - 255"} | ||
} | ||
|
||
color = Color{ | ||
R: r, | ||
G: g, | ||
B: b, | ||
W: w, | ||
} | ||
|
||
return color, 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
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,49 @@ | ||
package lights | ||
|
||
// Value represents the string value used to present a color. | ||
type Value struct { | ||
value string | ||
} | ||
|
||
// NewValue initialize a string or hex representation of a Color | ||
func NewValue(value string) (Value, error) { | ||
return Value{value: value}, nil | ||
} | ||
|
||
// GetColorByHex attempt's to find a matching Color using the value as a hex value. | ||
func (c *Value) GetColorByHex() (Color, error) { | ||
var color Color | ||
|
||
if c.value != "" { | ||
color, err := HexToColor(c.value) | ||
if err == nil { | ||
return color, nil | ||
} | ||
} | ||
|
||
return color, &InvalidColorError{c.value, "Value is invalid"} | ||
} | ||
|
||
// GetColorByName attempt's to find a matching Color using the value as a color preset. | ||
func (c *Value) GetColorByName() (Color, error) { | ||
var color Color | ||
|
||
if c.value == "" { | ||
return color, &InvalidColorError{c.value, "Value is invalid"} | ||
} | ||
|
||
cn, _ := lookupColorByName(c.value) | ||
if cn != nil { | ||
return *cn, nil | ||
} | ||
|
||
cl, err := lookupPrimaryColor(c.value) | ||
if err != nil { | ||
return *cl, &InvalidColorError{c.value, "Value is invalid"} | ||
} | ||
if cl != nil { | ||
return *cl, nil | ||
} | ||
|
||
return color, &InvalidColorError{c.value, "Value is invalid"} | ||
} |