7 Commits

Author SHA1 Message Date
e5b35f3fcc Improvements to Optional type 2024-10-30 23:54:11 -04:00
e6a94c487e Add a variadic constructor for container.Set 2024-09-20 17:07:16 -04:00
0f903cc8ec Upgrade to go 1.22 2024-09-12 03:25:16 -04:00
19fcdb4a7d Test more things in color 2024-09-12 03:18:42 -04:00
6bfa97e6aa Fix color.Premultiply 2024-09-12 03:13:35 -04:00
f647b544e2 Add tests for some color functions 2024-09-12 03:13:15 -04:00
1911987c59 Downgrade to go1.20 2024-09-12 03:12:43 -04:00
5 changed files with 65 additions and 22 deletions

View File

@@ -1,31 +1,31 @@
package ucontainer
// Optional is an optional value.
// Optional can either hold a value, or nothing.
type Optional[T any] struct {
value T
exists bool
}
// Value returns the value and true if the value exists. If not, it returns the
// last set value and false.
func (this *Optional[T]) Value () (T, bool) {
return this.value, this.exists
// O creates a new optional with the specified value.
func O[T any] (value T) Optional[T] {
return Optional[T] {
value: value,
exists: true,
}
}
// Set sets the value.
func (this *Optional[T]) Set (value T) {
this.value = value
this.exists = true
}
// Unset unsets the value.
func (this *Optional[T]) Unset () {
var zero T
this.value = zero
this.exists = false
// Void returns an optional with no value.
func Void[T any] () Optional[T] {
return Optional[T] { }
}
// Exists returns if the value is currently set.
func (this *Optional[T]) Exists () bool {
return this.exists
func (optional Optional[T]) Exists () bool {
return optional.exists
}
// Value returns the value and true if the value exists. If not, it returns the
// zero value and false.
func (optional Optional[T]) Value () (T, bool) {
return optional.value, optional.exists
}

View File

@@ -3,6 +3,15 @@ package ucontainer
// Set is a set of unique items, built on top of map.
type Set[T comparable] map[T] struct { }
// NewSet creates a new set that contains all specified items.
func NewSet[T comparable] (items ...T) Set[T] {
set := make(Set[T])
for _, item := range items {
set.Add(item)
}
return set
}
// Empty returns true if there are no items in the set.
func (set Set[T]) Empty () bool {
return set == nil || len(set) == 0

2
go.mod
View File

@@ -1,3 +1,3 @@
module git.tebibyte.media/sashakoshka/goutil
go 1.21.0
go 1.22.0

View File

@@ -22,9 +22,9 @@ func ToRGBA (c color.Color) color.RGBA {
// Premultiply applies alpha-premultiplication that colors must apply as per
// color.Color.RGBA. This is an intrinsically lossy proccess.
func Premultiply (ur, ug, ub, ua uint32) (r, g, b, a uint32) {
return (ur * a) / 0xFFFF,
(ug * a) / 0xFFFF,
(ub * a) / 0xFFFF,
return (ur * ua) / 0xFFFF,
(ug * ua) / 0xFFFF,
(ub * ua) / 0xFFFF,
ua
}

34
image/color/color_test.go Normal file
View File

@@ -0,0 +1,34 @@
package ucolor
import "testing"
import "image/color"
func TestTransparent (test *testing.T) {
if Transparent(color.NRGBA { A: 255 }) {
test.Fatal("false positive")
}
if !Transparent(color.NRGBA { A: 0 }) {
test.Fatal("false negative")
}
}
func TestToRGBA (test *testing.T) {
rgba := ToRGBA(color.NRGBA { R: 123, G: 100, B: 23, A: 230 })
if rgba != (color.RGBA { R: 111, G: 90, B: 20, A: 230 }) {
test.Fatalf("wrong value: %v", rgba)
}
}
func TestPremultiply (test *testing.T) {
r, g, b, a := Premultiply(0xFFFF, 0xFFFF, 0xFFFF, 0x8888)
if r != 0x8888 || g != 0x8888 || b != 0x8888 || a != 0x8888 {
test.Fatalf("wrong value: %08x %08x %08x %08x", r, g, b, a)
}
}
func TestUnpremultiply (test *testing.T) {
r, g, b, a := Unpremultiply(0x8888, 0x8888, 0x8888, 0x8888)
if r != 0xFFFF || g != 0xFFFF || b != 0xFFFF || a != 0x8888 {
test.Fatalf("wrong value: %08x %08x %08x %08x", r, g, b, a)
}
}