| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package main |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"image" |
| 25 |
"image/color" |
| 26 |
"image/draw" |
| 27 |
"image/png" |
| 28 |
"log" |
| 29 |
"os" |
| 30 |
|
| 31 |
"golang.org/x/exp/shiny/unit" |
| 32 |
"golang.org/x/exp/shiny/widget" |
| 33 |
"golang.org/x/exp/shiny/widget/node" |
| 34 |
"golang.org/x/exp/shiny/widget/theme" |
| 35 |
) |
| 36 |
|
| 37 |
var px = unit.Pixels |
| 38 |
|
| 39 |
func colorPatch(c color.Color, w, h unit.Value) *widget.Sizer { |
| 40 |
return widget.NewSizer(w, h, widget.NewUniform(theme.StaticColor(c), nil)) |
| 41 |
} |
| 42 |
|
| 43 |
func main() { |
| 44 |
t := theme.Default |
| 45 |
|
| 46 |
|
| 47 |
hf := widget.NewFlow(widget.AxisHorizontal, |
| 48 |
widget.NewLabel("Cyan:"), |
| 49 |
widget.WithLayoutData( |
| 50 |
colorPatch(color.RGBA{0x00, 0x7f, 0x7f, 0xff}, px(0), px(20)), |
| 51 |
widget.FlowLayoutData{AlongWeight: 1, ExpandAlong: true}, |
| 52 |
), |
| 53 |
widget.NewLabel("Magenta:"), |
| 54 |
widget.WithLayoutData( |
| 55 |
colorPatch(color.RGBA{0x7f, 0x00, 0x7f, 0xff}, px(0), px(30)), |
| 56 |
widget.FlowLayoutData{AlongWeight: 2, ExpandAlong: true}, |
| 57 |
), |
| 58 |
widget.NewLabel("Yellow:"), |
| 59 |
widget.WithLayoutData( |
| 60 |
colorPatch(color.RGBA{0x7f, 0x7f, 0x00, 0xff}, px(0), px(40)), |
| 61 |
widget.FlowLayoutData{AlongWeight: 3, ExpandAlong: true}, |
| 62 |
), |
| 63 |
) |
| 64 |
|
| 65 |
vf := widget.NewFlow(widget.AxisVertical, |
| 66 |
colorPatch(color.RGBA{0xff, 0x00, 0x00, 0xff}, px(80), px(40)), |
| 67 |
colorPatch(color.RGBA{0x00, 0xff, 0x00, 0xff}, px(50), px(50)), |
| 68 |
colorPatch(color.RGBA{0x00, 0x00, 0xff, 0xff}, px(20), px(60)), |
| 69 |
widget.WithLayoutData( |
| 70 |
hf, |
| 71 |
widget.FlowLayoutData{ExpandAcross: true}, |
| 72 |
), |
| 73 |
widget.NewLabel(fmt.Sprintf( |
| 74 |
"The black rectangle is 1.5 inches x 1 inch when viewed at %v DPI.", t.GetDPI())), |
| 75 |
widget.NewPadder(widget.AxisBoth, unit.Pixels(8), |
| 76 |
colorPatch(color.Black, unit.Inches(1.5), unit.Inches(1)), |
| 77 |
), |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
const width, height = 640, 480 |
| 82 |
rgba := image.NewRGBA(image.Rect(0, 0, width, height)) |
| 83 |
draw.Draw(rgba, rgba.Bounds(), t.GetPalette().Neutral(), image.Point{}, draw.Src) |
| 84 |
|
| 85 |
|
| 86 |
vf.Measure(t, width, height) |
| 87 |
vf.Rect = rgba.Bounds() |
| 88 |
vf.Layout(t) |
| 89 |
vf.PaintBase(&node.PaintBaseContext{ |
| 90 |
Theme: t, |
| 91 |
Dst: rgba, |
| 92 |
}, image.Point{}) |
| 93 |
|
| 94 |
|
| 95 |
out, err := os.Create("out.png") |
| 96 |
if err != nil { |
| 97 |
log.Fatalf("os.Create: %v", err) |
| 98 |
} |
| 99 |
defer out.Close() |
| 100 |
if err := png.Encode(out, rgba); err != nil { |
| 101 |
log.Fatalf("png.Encode: %v", err) |
| 102 |
} |
| 103 |
fmt.Println("Wrote out.png OK.") |
| 104 |
} |