The Go Programming Language

Source file src/pkg/image/names.go

     1	// Copyright 2010 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package image
     6	
     7	var (
     8		// Black is an opaque black ColorImage.
     9		Black = NewColorImage(Gray16Color{0})
    10		// White is an opaque white ColorImage.
    11		White = NewColorImage(Gray16Color{0xffff})
    12		// Transparent is a fully transparent ColorImage.
    13		Transparent = NewColorImage(Alpha16Color{0})
    14		// Opaque is a fully opaque ColorImage.
    15		Opaque = NewColorImage(Alpha16Color{0xffff})
    16	)
    17	
    18	// A ColorImage is an infinite-sized Image of uniform Color.
    19	// It implements both the Color and Image interfaces.
    20	type ColorImage struct {
    21		C Color
    22	}
    23	
    24	func (c *ColorImage) RGBA() (r, g, b, a uint32) {
    25		return c.C.RGBA()
    26	}
    27	
    28	func (c *ColorImage) ColorModel() ColorModel {
    29		return ColorModelFunc(func(Color) Color { return c.C })
    30	}
    31	
    32	func (c *ColorImage) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }
    33	
    34	func (c *ColorImage) At(x, y int) Color { return c.C }
    35	
    36	// Opaque scans the entire image and returns whether or not it is fully opaque.
    37	func (c *ColorImage) Opaque() bool {
    38		_, _, _, a := c.C.RGBA()
    39		return a == 0xffff
    40	}
    41	
    42	func NewColorImage(c Color) *ColorImage {
    43		return &ColorImage{c}
    44	}
    45	
    46	// A Tiled is an infinite-sized Image that repeats another Image in both
    47	// directions. Tiled{i, p}.At(x, y) will equal i.At(x+p.X, y+p.Y) for all
    48	// points {x+p.X, y+p.Y} within i's Bounds.
    49	type Tiled struct {
    50		I      Image
    51		Offset Point
    52	}
    53	
    54	func (t *Tiled) ColorModel() ColorModel {
    55		return t.I.ColorModel()
    56	}
    57	
    58	func (t *Tiled) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} }
    59	
    60	func (t *Tiled) At(x, y int) Color {
    61		p := Point{x, y}.Add(t.Offset).Mod(t.I.Bounds())
    62		return t.I.At(p.X, p.Y)
    63	}
    64	
    65	func NewTiled(i Image, offset Point) *Tiled {
    66		return &Tiled{i, offset}
    67	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.