• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fyne-io / fyne / 18807176232

25 Oct 2025 06:34PM UTC coverage: 61.057% (-0.004%) from 61.061%
18807176232

Pull #5989

github

Jacalz
Fix TODO regarding comparable map key
Pull Request #5989: RFC: Proof of concept for upgrading Go to 1.24

155 of 188 new or added lines in 62 files covered. (82.45%)

27 existing lines in 6 files now uncovered.

25609 of 41943 relevant lines covered (61.06%)

692.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.78
/canvas/raster.go
1
package canvas
2

3
import (
4
        "image"
5
        "image/color"
6
        "image/draw"
7

8
        "fyne.io/fyne/v2"
9
)
10

11
// Declare conformity with CanvasObject interface
12
var _ fyne.CanvasObject = (*Raster)(nil)
13

14
// Raster describes a raster image area that can render in a Fyne canvas
15
type Raster struct {
16
        baseObject
17

18
        // Render the raster image from code
19
        Generator func(w, h int) image.Image
20

21
        // Set a translucency value > 0.0 to fade the raster
22
        Translucency float64
23
        // Specify the type of scaling interpolation applied to the raster if it is not full-size
24
        // Since: 1.4.1
25
        ScaleMode ImageScale
26
}
27

28
// Alpha is a convenience function that returns the alpha value for a raster
29
// based on its Translucency value. The result is 1.0 - Translucency.
30
func (r *Raster) Alpha() float64 {
×
31
        return 1.0 - r.Translucency
×
32
}
×
33

34
// Hide will set this raster to not be visible
35
func (r *Raster) Hide() {
×
36
        r.baseObject.Hide()
×
37

×
38
        repaint(r)
×
39
}
×
40

41
// Move the raster to a new position, relative to its parent / canvas
42
func (r *Raster) Move(pos fyne.Position) {
×
43
        if r.Position() == pos {
×
44
                return
×
45
        }
×
46

47
        r.baseObject.Move(pos)
×
48

×
49
        repaint(r)
×
50
}
51

52
// Resize on a raster image causes the new size to be set and then calls Refresh.
53
// This causes the underlying data to be recalculated and a new output to be drawn.
54
func (r *Raster) Resize(s fyne.Size) {
×
55
        if s == r.Size() {
×
56
                return
×
57
        }
×
58

59
        r.baseObject.Resize(s)
×
60
        Refresh(r)
×
61
}
62

63
// Refresh causes this raster to be redrawn with its configured state.
64
func (r *Raster) Refresh() {
×
65
        Refresh(r)
×
66
}
×
67

68
// NewRaster returns a new Image instance that is rendered dynamically using
69
// the specified generate function.
70
// Images returned from this method should draw dynamically to fill the width
71
// and height parameters passed to pixelColor.
72
func NewRaster(generate func(w, h int) image.Image) *Raster {
×
73
        return &Raster{Generator: generate}
×
74
}
×
75

76
type pixelRaster struct {
77
        r *Raster
78

79
        img draw.Image
80
}
81

82
// NewRasterWithPixels returns a new Image instance that is rendered dynamically
83
// by iterating over the specified pixelColor function for each x, y pixel.
84
// Images returned from this method should draw dynamically to fill the width
85
// and height parameters passed to pixelColor.
86
func NewRasterWithPixels(pixelColor func(x, y, w, h int) color.Color) *Raster {
×
87
        pix := &pixelRaster{}
×
88
        pix.r = &Raster{
×
89
                Generator: func(w, h int) image.Image {
×
90
                        if pix.img == nil || pix.img.Bounds().Size().X != w || pix.img.Bounds().Size().Y != h {
×
91
                                // raster first pixel, figure out color type
×
92
                                var dst draw.Image
×
93
                                rect := image.Rect(0, 0, w, h)
×
94
                                switch pixelColor(0, 0, w, h).(type) {
×
95
                                case color.Alpha:
×
96
                                        dst = image.NewAlpha(rect)
×
97
                                case color.Alpha16:
×
98
                                        dst = image.NewAlpha16(rect)
×
99
                                case color.CMYK:
×
100
                                        dst = image.NewCMYK(rect)
×
101
                                case color.Gray:
×
102
                                        dst = image.NewGray(rect)
×
103
                                case color.Gray16:
×
104
                                        dst = image.NewGray16(rect)
×
105
                                case color.NRGBA:
×
106
                                        dst = image.NewNRGBA(rect)
×
107
                                case color.NRGBA64:
×
108
                                        dst = image.NewNRGBA64(rect)
×
109
                                case color.RGBA:
×
110
                                        dst = image.NewRGBA(rect)
×
111
                                case color.RGBA64:
×
112
                                        dst = image.NewRGBA64(rect)
×
113
                                default:
×
114
                                        dst = image.NewRGBA(rect)
×
115
                                }
116
                                pix.img = dst
×
117
                        }
118

NEW
119
                        for y := range h {
×
NEW
120
                                for x := range w {
×
121
                                        pix.img.Set(x, y, pixelColor(x, y, w, h))
×
122
                                }
×
123
                        }
124

125
                        return pix.img
×
126
                },
127
        }
128
        return pix.r
×
129
}
130

131
type subImg interface {
132
        SubImage(r image.Rectangle) image.Image
133
}
134

135
// NewRasterFromImage returns a new Raster instance that is rendered from the Go
136
// image.Image passed in.
137
// Rasters returned from this method will map pixel for pixel to the screen
138
// starting img.Bounds().Min pixels from the top left of the canvas object.
139
// Truncates rather than scales the image.
140
// If smaller than the target space, the image will be padded with zero-pixels to the target size.
141
func NewRasterFromImage(img image.Image) *Raster {
1✔
142
        return &Raster{
1✔
143
                Generator: func(w int, h int) image.Image {
2✔
144
                        bounds := img.Bounds()
1✔
145

1✔
146
                        rect := image.Rect(0, 0, w, h)
1✔
147

1✔
148
                        switch {
1✔
149
                        case w == bounds.Max.X && h == bounds.Max.Y:
×
150
                                return img
×
151
                        case w >= bounds.Max.X && h >= bounds.Max.Y:
1✔
152
                                // try quickly truncating
1✔
153
                                if sub, ok := img.(subImg); ok {
1✔
154
                                        return sub.SubImage(image.Rectangle{
×
155
                                                Min: bounds.Min,
×
156
                                                Max: image.Point{
×
157
                                                        X: bounds.Min.X + w,
×
158
                                                        Y: bounds.Min.Y + h,
×
159
                                                },
×
160
                                        })
×
161
                                }
×
162
                        default:
×
163
                                if !rect.Overlaps(bounds) {
×
164
                                        return image.NewUniform(color.RGBA{})
×
165
                                }
×
166
                                bounds = bounds.Intersect(rect)
×
167
                        }
168

169
                        // respect the user's pixel format (if possible)
170
                        var dst draw.Image
1✔
171
                        switch i := img.(type) {
1✔
172
                        case *image.Alpha:
×
173
                                dst = image.NewAlpha(rect)
×
174
                        case *image.Alpha16:
×
175
                                dst = image.NewAlpha16(rect)
×
176
                        case *image.CMYK:
×
177
                                dst = image.NewCMYK(rect)
×
178
                        case *image.Gray:
×
179
                                dst = image.NewGray(rect)
×
180
                        case *image.Gray16:
×
181
                                dst = image.NewGray16(rect)
×
182
                        case *image.NRGBA:
×
183
                                dst = image.NewNRGBA(rect)
×
184
                        case *image.NRGBA64:
×
185
                                dst = image.NewNRGBA64(rect)
×
186
                        case *image.Paletted:
×
187
                                dst = image.NewPaletted(rect, i.Palette)
×
188
                        case *image.RGBA:
×
189
                                dst = image.NewRGBA(rect)
×
190
                        case *image.RGBA64:
×
191
                                dst = image.NewRGBA64(rect)
×
192
                        default:
1✔
193
                                dst = image.NewRGBA(rect)
1✔
194
                        }
195

196
                        draw.Draw(dst, bounds, img, bounds.Min, draw.Over)
1✔
197
                        return dst
1✔
198
                },
199
        }
200
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc