Files
ebiten/vector/util_test.go
T

143 lines
4.5 KiB
Go

// Copyright 2023 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vector_test
import (
"image"
"image/color"
"testing"
"github.com/hajimehoshi/ebiten/v2"
t "github.com/hajimehoshi/ebiten/v2/internal/testing"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func TestMain(m *testing.M) {
t.MainWithRunLoop(m)
}
// Issue #2589
func TestLine0(t *testing.T) {
dst := ebiten.NewImage(16, 16)
vector.StrokeLine(dst, 0, 0, 0, 0, 2, color.White, true)
if got, want := dst.At(0, 0), (color.RGBA{}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}
// Issue #3270
func TestStrokeRectAntiAlias(t *testing.T) {
dst := ebiten.NewImage(16, 16)
vector.StrokeRect(dst, 0, 0, 16, 16, 2, color.White, true)
if got, want := dst.At(5, 5), (color.RGBA{}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}
// Issue #3330
func TestFillRectSubImage(t *testing.T) {
dst := ebiten.NewImage(16, 16)
dst2 := dst.SubImage(image.Rect(0, 0, 8, 8)).(*ebiten.Image)
vector.FillRect(dst2, 0, 0, 8, 8, color.White, true)
if got, want := dst.At(5, 5), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst2.At(5, 5), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
dst3 := dst2.SubImage(image.Rect(4, 4, 8, 8)).(*ebiten.Image)
vector.FillRect(dst3, 4, 4, 4, 4, color.Black, true)
if got, want := dst.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst2.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst3.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}
// Issue #3330
func TestFillCircleSubImage(t *testing.T) {
dst := ebiten.NewImage(16, 16)
dst2 := dst.SubImage(image.Rect(0, 0, 8, 8)).(*ebiten.Image)
vector.FillCircle(dst2, 4, 4, 4, color.White, true)
if got, want := dst.At(5, 5), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst2.At(5, 5), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
dst3 := dst2.SubImage(image.Rect(4, 4, 8, 8)).(*ebiten.Image)
vector.FillCircle(dst3, 6, 6, 4, color.Black, true)
if got, want := dst.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst2.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst3.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}
// Issue #3330
func TestFillPathSubImage(t *testing.T) {
dst := ebiten.NewImage(16, 16)
dst2 := dst.SubImage(image.Rect(0, 0, 8, 8)).(*ebiten.Image)
var p vector.Path
p.MoveTo(0, 0)
p.LineTo(8, 0)
p.LineTo(8, 8)
p.LineTo(0, 8)
p.Close()
op := &vector.DrawPathOptions{}
op.ColorScale.ScaleWithColor(color.White)
op.AntiAlias = true
vector.FillPath(dst2, &p, nil, op)
if got, want := dst.At(5, 5), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst2.At(5, 5), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
dst3 := dst2.SubImage(image.Rect(4, 4, 8, 8)).(*ebiten.Image)
var p2 vector.Path
p2.MoveTo(4, 4)
p2.LineTo(8, 4)
p2.LineTo(8, 8)
p2.LineTo(4, 8)
p2.Close()
op.ColorScale.Reset()
op.ColorScale.ScaleWithColor(color.Black)
vector.FillPath(dst3, &p2, nil, op)
if got, want := dst.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst2.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := dst3.At(5, 5), (color.RGBA{0x00, 0x00, 0x00, 0xff}); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}