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

mlange-42 / ark-pixel / 13725782068

07 Mar 2025 05:16PM CUT coverage: 28.089% (-19.3%) from 47.374%
13725782068

Pull #8

github

web-flow
Merge c22a6e346 into 2253cb584
Pull Request #8: Add first plot drawers

7 of 677 new or added lines in 11 files covered. (1.03%)

457 of 1627 relevant lines covered (28.09%)

55.34 hits per line

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

0.0
/plot/image.go
1
package plot
2

3
import (
4
        "image/color"
5

6
        pixel "github.com/gopxl/pixel/v2"
7
        "github.com/gopxl/pixel/v2/backends/opengl"
8
        "github.com/mazznoer/colorgrad"
9
        "github.com/mlange-42/ark-pixel/window"
10
        "github.com/mlange-42/ark-tools/observer"
11
        "github.com/mlange-42/ark/ecs"
12
)
13

14
// Image drawer.
15
//
16
// Draws an image from a Matrix observer.
17
// The image is scaled to the canvas extent, with preserved aspect ratio.
18
// Does not add plot axes etc.
19
type Image struct {
20
        Scale    float64            // Spatial scaling: cell size in screen pixels. Optional, default auto.
21
        Observer observer.Matrix    // Observer providing 2D matrix or grid data.
22
        Colors   colorgrad.Gradient // Colors for mapping values.
23
        Min      float64            // Minimum value for color mapping. Optional.
24
        Max      float64            // Maximum value for color mapping. Optional. Is set to 1.0 if both Min and Max are zero.
25
        slope    float64
26
        picture  *pixel.PictureData
27
}
28

29
// Initialize the system
NEW
30
func (i *Image) Initialize(w *ecs.World, win *opengl.Window) {
×
NEW
31
        i.Observer.Initialize(w)
×
NEW
32

×
NEW
33
        if i.Min == 0 && i.Max == 0 {
×
NEW
34
                i.Max = 1
×
NEW
35
        }
×
36

NEW
37
        i.slope = 1.0 / (i.Max - i.Min)
×
NEW
38

×
NEW
39
        width, height := i.Observer.Dims()
×
NEW
40
        i.picture = pixel.MakePictureData(pixel.R(0, 0, float64(width), float64(height)))
×
41
}
42

43
// Update the drawer.
NEW
44
func (i *Image) Update(w *ecs.World) {
×
NEW
45
        i.Observer.Update(w)
×
NEW
46
}
×
47

48
// UpdateInputs handles input events of the previous frame update.
NEW
49
func (i *Image) UpdateInputs(w *ecs.World, win *opengl.Window) {}
×
50

51
// Draw the system
NEW
52
func (i *Image) Draw(w *ecs.World, win *opengl.Window) {
×
NEW
53
        values := i.Observer.Values(w)
×
NEW
54

×
NEW
55
        length := len(values)
×
NEW
56
        for j := 0; j < length; j++ {
×
NEW
57
                i.picture.Pix[j] = i.valueToColor(values[j])
×
NEW
58
        }
×
59

NEW
60
        scale := i.Scale
×
NEW
61
        if i.Scale <= 0 {
×
NEW
62
                scale = window.Scale(win, i.picture.Rect.W(), i.picture.Rect.H())
×
NEW
63
        }
×
64

NEW
65
        sprite := pixel.NewSprite(i.picture, i.picture.Bounds())
×
NEW
66
        sprite.Draw(win,
×
NEW
67
                pixel.IM.Moved(pixel.V(i.picture.Rect.W()/2.0, i.picture.Rect.H()/2.0)).
×
NEW
68
                        Scaled(pixel.Vec{}, scale),
×
NEW
69
        )
×
70
}
71

NEW
72
func (i *Image) valueToColor(v float64) color.RGBA {
×
NEW
73
        c := i.Colors.At((v - i.Min) * i.slope)
×
NEW
74
        return color.RGBA{
×
NEW
75
                R: uint8(c.R * 255),
×
NEW
76
                G: uint8(c.G * 255),
×
NEW
77
                B: uint8(c.B * 255),
×
NEW
78
                A: 0xff,
×
NEW
79
        }
×
NEW
80
}
×
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