Skip to content

Commit

Permalink
Use bytes for r, g and b
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed May 14, 2024
1 parent 5e83a8c commit 2dfbc95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
17 changes: 9 additions & 8 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Box struct {
x, y int
w, h int
r, g, b, a int
r, g, b, a byte
}

// CreateRandomBox randomly searches for a place for a 1x1 size box.
Expand All @@ -24,7 +24,8 @@ type Box struct {
func (pi *PixelImage) CreateRandomBox(checkIfPossible bool) *Box {
w := 1
h := 1
var x, y, r, g, b, a int
var x, y int
var r, g, b, a byte
for !checkIfPossible || !pi.Done(0, 0) {
// Find a random placement for (x,y), for a box of size (1,1)
x = rand.Intn(pi.w)
Expand Down Expand Up @@ -155,16 +156,16 @@ func (pi *PixelImage) Expand(bo *Box) (expanded bool) {

// singleHex returns a single digit hex number, as a string
// the numbers are not rounded, just floored
func singleHex(x int) string {
func singleHex(x byte) string {
hex := strconv.FormatInt(int64(x), 16)
if len(hex) == 1 {
return "0"
if len(hex) < 2 {
return "0" + hex // Prepend "0" if the hex representation is a single digit
}
return string(hex[0])
return hex
}

// shortColorString returns a string representing a color on the short form "#000"
func shortColorString(r, g, b int) string {
func shortColorString(r, g, b byte) string {
return "#" + singleHex(r) + singleHex(g) + singleHex(b)
}

Expand All @@ -186,7 +187,7 @@ func (pi *PixelImage) CoverBox(bo *Box, pink bool, optimizeColors bool) {
} else if optimizeColors {
colorString = shortColorString(bo.r, bo.g, bo.b)
} else {
colorString = string(tinysvg.ColorBytes(bo.r, bo.g, bo.b))
colorString = string(tinysvg.ColorBytes(int(bo.r), int(bo.g), int(bo.b)))
}

// Set the fill color
Expand Down
20 changes: 10 additions & 10 deletions pixelimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
type Pixel struct {
x int
y int
r int
g int
b int
a int
r byte
g byte
b byte
a byte
covered bool
}

Expand Down Expand Up @@ -107,10 +107,10 @@ func NewPixelImage(img image.Image, verbose bool) *PixelImage {

for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
c = color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
alpha := int(c.A)
alpha := byte(c.A)
// Mark transparent pixels as already being "covered"
covered := alpha == 0
pixels[i] = &Pixel{x, y, int(c.R), int(c.G), int(c.B), alpha, covered}
pixels[i] = &Pixel{x, y, byte(c.R), byte(c.G), byte(c.B), alpha, covered}
i++
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func (pi *PixelImage) Done(startx, starty int) bool {
}

// At returns the RGB color at the given coordinate
func (pi *PixelImage) At(x, y int) (r, g, b int) {
func (pi *PixelImage) At(x, y int) (r, g, b byte) {
i := y*pi.w + x
//if i >= len(pi.pixels) {
// panic("At out of bounds, too large coordinate")
Expand All @@ -153,7 +153,7 @@ func (pi *PixelImage) At(x, y int) (r, g, b int) {
}

// At2 returns the RGBA color at the given coordinate
func (pi *PixelImage) At2(x, y int) (r, g, b, a int) {
func (pi *PixelImage) At2(x, y int) (r, g, b, a byte) {
i := y*pi.w + x
//if i >= len(pi.pixels) {
// panic("At out of bounds, too large coordinate")
Expand All @@ -175,7 +175,7 @@ func (pi *PixelImage) CoverAllPixels() {
coverCount := 0
for _, p := range pi.pixels {
if !(*p).covered {
pi.svgTag.Pixel((*p).x, (*p).y, (*p).r, (*p).g, (*p).b)
pi.svgTag.Pixel((*p).x, (*p).y, int((*p).r), int((*p).g), int((*p).b))
(*p).covered = true
coverCount++
}
Expand All @@ -194,7 +194,7 @@ func (pi *PixelImage) CoverAllPixelsCallback(callbackFunc func(int, int), Nth in
callbackFunc(0, l)
for i, p := range pi.pixels {
if !(*p).covered {
pi.svgTag.Pixel((*p).x, (*p).y, (*p).r, (*p).g, (*p).b)
pi.svgTag.Pixel((*p).x, (*p).y, int((*p).r), int((*p).g), int((*p).b))
(*p).covered = true
coverCount++
}
Expand Down

0 comments on commit 2dfbc95

Please sign in to comment.