Skip to content

Commit 25caa59

Browse files
committed
[devtools] Adjust pointer color to pixel underneath
1 parent f5ebdac commit 25caa59

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

devtools/internal/inspector/draw.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/elgopher/pi"
1010
"github.com/elgopher/pi/devtools/internal/icons"
11+
"github.com/elgopher/pi/devtools/internal/rgb"
1112
"github.com/elgopher/pi/devtools/internal/snapshot"
1213
"github.com/elgopher/pi/vm"
1314
)
@@ -55,5 +56,14 @@ func printPixelColor(color byte, x int, y int) int {
5556

5657
func drawPointer() {
5758
x, y := pi.MousePos()
58-
icons.Draw(icons.Pointer, x, y, FgColor)
59+
icons.Draw(icons.Pointer, x, y, choosePointerColor(x, y))
60+
}
61+
62+
func choosePointerColor(x, y int) byte {
63+
c := pi.Pget(x, y)
64+
if rgb.BrightnessDelta(vm.Palette[FgColor], vm.Palette[c]) >= rgb.BrightnessDelta(vm.Palette[BgColor], vm.Palette[c]) {
65+
return FgColor
66+
}
67+
68+
return BgColor
5969
}

devtools/internal/rgb/rgb.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package rgb
2+
3+
import "github.com/elgopher/pi/vm"
4+
5+
func BrightnessIndex(rgb vm.RGB) byte {
6+
return byte((299*int(rgb.R) + 587*int(rgb.G) + 114*int(rgb.B)) / 1000)
7+
}
8+
9+
func BrightnessDelta(rgb1, rgb2 vm.RGB) byte {
10+
i1 := BrightnessIndex(rgb1)
11+
i2 := BrightnessIndex(rgb2)
12+
if i1 > i2 {
13+
return i1 - i2
14+
}
15+
16+
return i2 - i1
17+
}

0 commit comments

Comments
 (0)