summaryrefslogtreecommitdiff
path: root/libgo/go/image/names.go
blob: c309684cea13b8618607a4bad9d8383995728b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package image

var (
	// Black is an opaque black ColorImage.
	Black = NewColorImage(Gray16Color{0})
	// White is an opaque white ColorImage.
	White = NewColorImage(Gray16Color{0xffff})
	// Transparent is a fully transparent ColorImage.
	Transparent = NewColorImage(Alpha16Color{0})
	// Opaque is a fully opaque ColorImage.
	Opaque = NewColorImage(Alpha16Color{0xffff})
)

// A ColorImage is an infinite-sized Image of uniform Color.
// It implements both the Color and Image interfaces.
type ColorImage struct {
	C Color
}

func (c *ColorImage) RGBA() (r, g, b, a uint32) {
	return c.C.RGBA()
}

func (c *ColorImage) ColorModel() ColorModel {
	return ColorModelFunc(func(Color) Color { return c.C })
}

func (c *ColorImage) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }

func (c *ColorImage) At(x, y int) Color { return c.C }

// Opaque scans the entire image and returns whether or not it is fully opaque.
func (c *ColorImage) Opaque() bool {
	_, _, _, a := c.C.RGBA()
	return a == 0xffff
}

func NewColorImage(c Color) *ColorImage {
	return &ColorImage{c}
}

// A Tiled is an infinite-sized Image that repeats another Image in both
// directions. Tiled{i, p}.At(x, y) will equal i.At(x+p.X, y+p.Y) for all
// points {x+p.X, y+p.Y} within i's Bounds.
type Tiled struct {
	I      Image
	Offset Point
}

func (t *Tiled) ColorModel() ColorModel {
	return t.I.ColorModel()
}

func (t *Tiled) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }

func (t *Tiled) At(x, y int) Color {
	p := Point{x, y}.Add(t.Offset).Mod(t.I.Bounds())
	return t.I.At(p.X, p.Y)
}

func NewTiled(i Image, offset Point) *Tiled {
	return &Tiled{i, offset}
}