...
Source file
src/tawesoft.co.uk/go/ximage/red.go
Documentation:
src/tawesoft.co.uk/go/ximage/red.go
1
2
3
4
5
6 package ximage
7
8 import (
9 "image"
10 "image/color"
11 "tawesoft.co.uk/go/ximage/xcolor"
12 )
13
14
15 type Red struct {
16
17
18 Pix []uint8
19
20 Stride int
21
22 Rect image.Rectangle
23 }
24
25 func (p *Red) ColorModel() color.Model { return xcolor.RedModel }
26
27 func (p *Red) Bounds() image.Rectangle { return p.Rect }
28
29 func (p *Red) At(x, y int) color.Color {
30 return p.RedAt(x, y)
31 }
32
33 func (p *Red) RedAt(x, y int) xcolor.Red {
34 if !(image.Point{x, y}.In(p.Rect)) {
35 return xcolor.Red{}
36 }
37 i := p.PixOffset(x, y)
38 return xcolor.Red{R: p.Pix[i]}
39 }
40
41
42
43 func (p *Red) PixOffset(x, y int) int {
44 return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*1
45 }
46
47 func (p *Red) Set(x, y int, c color.Color) {
48 if !(image.Point{x, y}.In(p.Rect)) {
49 return
50 }
51 i := p.PixOffset(x, y)
52 p.Pix[i] = xcolor.RedModel.Convert(c).(color.RGBA).R
53 }
54
55 func (p *Red) SetRed(x, y int, c xcolor.Red) {
56 if !(image.Point{x, y}.In(p.Rect)) {
57 return
58 }
59 i := p.PixOffset(x, y)
60 p.Pix[i] = c.R
61 }
62
63
64
65 func (p *Red) SubImage(r image.Rectangle) image.Image {
66 r = r.Intersect(p.Rect)
67
68
69
70 if r.Empty() {
71 return &Red{}
72 }
73 i := p.PixOffset(r.Min.X, r.Min.Y)
74 return &Red{
75 Pix: p.Pix[i:],
76 Stride: p.Stride,
77 Rect: r,
78 }
79 }
80
81
82 func (p *Red) Opaque() bool {
83 return true
84 }
85
86
87 func NewRed(r image.Rectangle) *Red {
88 w, h := r.Dx(), r.Dy()
89 pix := make([]uint8, 1*w*h)
90 return &Red{pix, 1 * w, r}
91 }
92
View as plain text