Random Color Generator – – Fyne GUI Golang tutorial 23 & 24

This is a complete Fyne & Golang Project.

No need of prior experience.

The idea of this app is to create random colors.

  • Shades of Red color.
  • Shades of Blue color
  • shades of Green color.

How to create random colors ?

rand.Intn() fuction is used to create a random number.

So this function is used to create a random digit between zero and 255.

Note: You need to import math/rand library and crypt/rand will not work in this project.

 

How to convert to uint8?

uint8() function is used to convert string or integer to UINT8 format.

 

GUI Elements

A basic canvas element like rectange is used in this app and 4 simple buttons.

 

 

Part #2

Source Code

[sourcecode lang=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Code main.go”]  
package main

// import fyne
import (
"image/color"
"math/rand"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)

func main() {
// New app
a := app.New()
// Title and New window
w := a.NewWindow("Random Color Generator")
// Resize window
w.Resize(fyne.NewSize(400, 400))
// New Rectange
// Color for new Rectange
colorx := color.NRGBA{R: 0, G: 0, B: 0, A: 255}
rect1 := canvas.NewRectangle(colorx)
rect1.SetMinSize(fyne.NewSize(200, 200))
// Btn for color change
btn1 := widget.NewButton("Random color", func() {
// UI is done.. Now Logic
// unit8 is necessary to convert int to uint8
rect1.FillColor = color.NRGBA{R: uint8(rand.Intn(255)),
G: uint8(rand.Intn(255)), B: uint8(rand.Intn(255)), A: 255}
rect1.Refresh() // refresh screen
})
// Random RED Changer
btnRed := widget.NewButton("Random RED", func() {
// UI is done.. Now Logic
// unit8 is necessary to convert int to uint8
rect1.FillColor = color.NRGBA{R: uint8(rand.Intn(255)),
G: 0, B: 0, A: 255}
rect1.Refresh() // refresh screen
})
// Random GREEN COLOR
btnGreen := widget.NewButton("Random Green", func() {
// UI is done.. Now Logic
// unit8 is necessary to convert int to uint8
rect1.FillColor = color.NRGBA{R: 0,
G: uint8(rand.Intn(255)), B: 0, A: 255}
rect1.Refresh() // refresh screen
})
// Random Blue COLOR
btnBlue := widget.NewButton("Random Blue", func() {
// UI is done.. Now Logic
// unit8 is necessary to convert int to uint8
rect1.FillColor = color.NRGBA{R: 0,
G: 0, B: uint8(rand.Intn(255)), A: 255}
rect1.Refresh() // refresh screen
})
w.SetContent(
container.NewVBox(
rect1,
btn1,
btnRed,
btnGreen,
btnBlue,
),
)
w.ShowAndRun()
}

[/sourcecode] [easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main23-24.go” text=”Download Code” color=”red_darker”]

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: