add variable system + fix bugs here and there.
This commit is contained in:
+21
-18
@@ -3,7 +3,9 @@ package color
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer3/server/internal/gentools"
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -56,10 +58,6 @@ func (col *Color) SetXY(xy XY) {
|
||||
}
|
||||
|
||||
func (col *Color) AlmostEquals(other Color) bool {
|
||||
if (col.K != nil) != (other.K != nil) {
|
||||
return false
|
||||
}
|
||||
|
||||
if col.HS != nil && other.HS != nil {
|
||||
if math.Abs(col.HS.Hue-other.HS.Hue) > 0.01 {
|
||||
return false
|
||||
@@ -68,7 +66,7 @@ func (col *Color) AlmostEquals(other Color) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if col.K != nil && *col.K != *other.K {
|
||||
if col.K != nil && other.K != nil && *col.K != *other.K {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -76,6 +74,10 @@ func (col *Color) AlmostEquals(other Color) bool {
|
||||
}
|
||||
|
||||
if col.K != nil {
|
||||
if other.K != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return *col.K == *other.K
|
||||
}
|
||||
|
||||
@@ -94,15 +96,8 @@ func (col *Color) AlmostEquals(other Color) bool {
|
||||
}
|
||||
|
||||
xy1, _ := col.ToXY()
|
||||
xy2, _ := col.ToXY()
|
||||
if math.Abs(xy1.XY.X-xy2.XY.X) > 0.001 {
|
||||
return false
|
||||
}
|
||||
if math.Abs(xy1.XY.Y-xy2.XY.Y) > 0.001 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
xy2, _ := other.ToXY()
|
||||
return math.Abs(xy1.XY.X-xy2.XY.X)+math.Abs(xy1.XY.Y-xy2.XY.Y) < 0.001
|
||||
}
|
||||
|
||||
// ToRGB tries to copy the color to an RGB color. If it's already RGB, it will be plainly copied, but HS
|
||||
@@ -217,15 +212,20 @@ func (col *Color) ToXY() (col2 Color, ok bool) {
|
||||
|
||||
func (col *Color) Interpolate(other Color, fac float64) Color {
|
||||
if col.AlmostEquals(other) {
|
||||
log.Println("ALMOST EQUAL", col.String(), other.String())
|
||||
return *col
|
||||
}
|
||||
|
||||
// Special case for kelvin values.
|
||||
// Special case for both kelvin values.
|
||||
if col.IsKelvin() && other.IsKelvin() {
|
||||
k1 := *col.K
|
||||
k2 := *col.K
|
||||
k3 := k1 + int(float64(k2-k1)*fac)
|
||||
return Color{K: &k3}
|
||||
k2 := *other.K
|
||||
|
||||
if k2 > k1 {
|
||||
return Color{K: gentools.Ptr(k1 + int(float64(k2-k1)*fac))}
|
||||
} else {
|
||||
return Color{K: gentools.Ptr(k1 - int(float64(k1-k2)*fac))}
|
||||
}
|
||||
}
|
||||
|
||||
if fac < 0.000001 {
|
||||
@@ -292,6 +292,9 @@ func (col *Color) String() string {
|
||||
|
||||
func (col *Color) colorful() colorful.Color {
|
||||
switch {
|
||||
case col.K != nil:
|
||||
col2, _ := col.ToXY()
|
||||
return col2.colorful()
|
||||
case col.HS != nil:
|
||||
return colorful.Hsv(col.HS.Hue, col.HS.Sat, 1)
|
||||
case col.RGB != nil:
|
||||
|
||||
Reference in New Issue
Block a user