|
@@ -1,20 +1,21 @@
|
|
|
// Copyright 2016 The G3N Authors. All rights reserved.
|
|
// Copyright 2016 The G3N Authors. All rights reserved.
|
|
|
// Use of this source code is governed by a BSD-style
|
|
// Use of this source code is governed by a BSD-style
|
|
|
// license that can be found in the LICENSE file.
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
+
|
|
|
package math32
|
|
package math32
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"strings"
|
|
"strings"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-// Type color describes an RGB color
|
|
|
|
|
|
|
+// Color describes an RGB color
|
|
|
type Color struct {
|
|
type Color struct {
|
|
|
R float32
|
|
R float32
|
|
|
G float32
|
|
G float32
|
|
|
B float32
|
|
B float32
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// New creates and returns a pointer to a new Color
|
|
|
|
|
|
|
+// NewColor creates and returns a pointer to a new Color
|
|
|
// with the specified web standard color name (case insensitive).
|
|
// with the specified web standard color name (case insensitive).
|
|
|
// Returns nil if the color name not found
|
|
// Returns nil if the color name not found
|
|
|
func NewColor(name string) *Color {
|
|
func NewColor(name string) *Color {
|
|
@@ -26,8 +27,7 @@ func NewColor(name string) *Color {
|
|
|
return &c
|
|
return &c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Name returns a Color with the specified standard web color
|
|
|
|
|
-// name (case insensitive).
|
|
|
|
|
|
|
+// ColorName returns a Color with the specified standard web color name (case insensitive).
|
|
|
// Returns black color if the specified color name not found
|
|
// Returns black color if the specified color name not found
|
|
|
func ColorName(name string) Color {
|
|
func ColorName(name string) Color {
|
|
|
|
|
|
|
@@ -71,6 +71,8 @@ func (c *Color) SetName(name string) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Add adds to each RGB component of this color the correspondent component of other color
|
|
|
|
|
+// Returns pointer to this updated color
|
|
|
func (c *Color) Add(other *Color) *Color {
|
|
func (c *Color) Add(other *Color) *Color {
|
|
|
|
|
|
|
|
c.R += other.R
|
|
c.R += other.R
|
|
@@ -79,6 +81,8 @@ func (c *Color) Add(other *Color) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// AddColors adds to each RGB component of this color the correspondent component of color1 and color2
|
|
|
|
|
+// Returns pointer to this updated color
|
|
|
func (c *Color) AddColors(color1, color2 *Color) *Color {
|
|
func (c *Color) AddColors(color1, color2 *Color) *Color {
|
|
|
|
|
|
|
|
c.R = color1.R + color2.R
|
|
c.R = color1.R + color2.R
|
|
@@ -87,6 +91,8 @@ func (c *Color) AddColors(color1, color2 *Color) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// AddScalar adds the specified scalar value to each RGB component of this color
|
|
|
|
|
+// Returns pointer to this updated color
|
|
|
func (c *Color) AddScalar(s float32) *Color {
|
|
func (c *Color) AddScalar(s float32) *Color {
|
|
|
|
|
|
|
|
c.R += s
|
|
c.R += s
|
|
@@ -95,6 +101,8 @@ func (c *Color) AddScalar(s float32) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Multiply multiplies each RGB component of this color by other
|
|
|
|
|
+// Returns pointer to this updated color
|
|
|
func (c *Color) Multiply(other *Color) *Color {
|
|
func (c *Color) Multiply(other *Color) *Color {
|
|
|
|
|
|
|
|
c.R *= other.R
|
|
c.R *= other.R
|
|
@@ -103,6 +111,8 @@ func (c *Color) Multiply(other *Color) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// MultiplyScalar multiplies each RGB component of this color by the specified scalar.
|
|
|
|
|
+// Returns pointer to this updated color
|
|
|
func (c *Color) MultiplyScalar(v float32) *Color {
|
|
func (c *Color) MultiplyScalar(v float32) *Color {
|
|
|
|
|
|
|
|
c.R *= v
|
|
c.R *= v
|
|
@@ -111,6 +121,9 @@ func (c *Color) MultiplyScalar(v float32) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Lerp linear sets this color as the linear interpolation of itself
|
|
|
|
|
+// with the specified color for the specified alpha.
|
|
|
|
|
+// Returns pointer to this updated color
|
|
|
func (c *Color) Lerp(color *Color, alpha float32) *Color {
|
|
func (c *Color) Lerp(color *Color, alpha float32) *Color {
|
|
|
|
|
|
|
|
c.R += (color.R - c.R) * alpha
|
|
c.R += (color.R - c.R) * alpha
|
|
@@ -119,11 +132,13 @@ func (c *Color) Lerp(color *Color, alpha float32) *Color {
|
|
|
return c
|
|
return c
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Equals returns if this color is equal to other
|
|
|
func (c *Color) Equals(other *Color) bool {
|
|
func (c *Color) Equals(other *Color) bool {
|
|
|
|
|
|
|
|
return (c.R == other.R) && (c.G == other.G) && (c.B == other.B)
|
|
return (c.R == other.R) && (c.G == other.G) && (c.B == other.B)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// IsColorName returns if the specified name is valid color name
|
|
|
func IsColorName(name string) (Color, bool) {
|
|
func IsColorName(name string) (Color, bool) {
|
|
|
|
|
|
|
|
c, ok := mapColorNames[strings.ToLower(name)]
|
|
c, ok := mapColorNames[strings.ToLower(name)]
|