Icon style (colorized or grayscale), part 2, README update

This commit is contained in:
2026-11-06 16:00:43 +00:00
parent ecde38f9e8
commit 9e6b24268f
3 changed files with 201 additions and 2 deletions
+198
View File
@@ -0,0 +1,198 @@
package main
// Icon style: colorized or grayscale. It applies to the app
// icons (their pixels are filtered) and to everything MirrorBar draws itself
// (the logo, clock, battery, volume, wifi and arrow colors).
import (
"os"
"path/filepath"
"strconv"
"strings"
"unsafe"
)
const (
modeColor = 0
modeGray = 1
menuStyleBase = 160
)
var (
pGetIconInfo = user32.NewProc("GetIconInfo")
pCreateIconIndirect = user32.NewProc("CreateIconIndirect")
pDestroyIcon = user32.NewProc("DestroyIcon")
pGetObject = gdi32.NewProc("GetObjectW")
pGetDIBits = gdi32.NewProc("GetDIBits")
iconMode = modeColor
styleCache = map[styleKey]uintptr{}
)
type styleKey struct {
icon uintptr
mode int
}
type iconInfo struct {
FIcon int32
XHot uint32
YHot uint32
Mask uintptr
Color uintptr
}
type bitmapObj struct {
Type int32
Width int32
Height int32
WidthBytes int32
Planes uint16
BitsPixel uint16
Bits uintptr
}
var styleNames = []string{"Colorized", "Grayscale"}
func styleFile() string { return filepath.Join(dataDir(), "iconstyle.txt") }
func loadIconMode() {
b, err := os.ReadFile(styleFile())
if err != nil {
return
}
if n, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && n >= 0 && n < len(styleNames) {
iconMode = n
}
}
func saveIconMode() {
_ = os.MkdirAll(dataDir(), 0o755)
_ = os.WriteFile(styleFile(), []byte(strconv.Itoa(iconMode)), 0o644)
}
// styleCacheClear throws away the filtered copies of icons.
func styleCacheClear() {
for _, v := range styleCache {
if v != 0 {
pDestroyIcon.Call(v)
}
}
styleCache = map[styleKey]uintptr{}
}
// styledIcon is the icon as it should be drawn in the current style.
func styledIcon(ic uintptr) uintptr {
if ic == 0 || iconMode == modeColor {
return ic
}
k := styleKey{ic, iconMode}
if v, ok := styleCache[k]; ok {
if v != 0 {
return v
}
return ic
}
v := makeStyledIcon(ic, iconMode)
if len(styleCache) > 300 {
styleCacheClear()
}
styleCache[k] = v
if v == 0 {
return ic
}
return v
}
// makeStyledIcon returns a filtered copy of an icon, or 0 if it can't be done
// (the original is then drawn as it is).
func makeStyledIcon(ic uintptr, mode int) (result uintptr) {
defer func() {
if r := recover(); r != nil {
result = 0
}
}()
var ii iconInfo
r, _, _ := pGetIconInfo.Call(ic, uintptr(unsafe.Pointer(&ii)))
if r == 0 {
return 0
}
defer func() {
if ii.Mask != 0 {
pDeleteObject.Call(ii.Mask)
}
if ii.Color != 0 {
pDeleteObject.Call(ii.Color)
}
}()
if ii.Color == 0 {
return 0 // a two-color icon, leave it
}
var bm bitmapObj
pGetObject.Call(ii.Color, unsafe.Sizeof(bm), uintptr(unsafe.Pointer(&bm)))
w, h := int(bm.Width), int(bm.Height)
if w <= 0 || h <= 0 || w > 512 || h > 512 {
return 0
}
hdc, _, _ := pGetDC.Call(0)
defer pReleaseDC.Call(0, hdc)
bmi := bitmapInfoHeader{Width: int32(w), Height: int32(-h), Planes: 1, BitCount: 32}
bmi.Size = uint32(unsafe.Sizeof(bmi))
pix := make([]uint32, w*h)
n, _, _ := pGetDIBits.Call(hdc, ii.Color, 0, uintptr(h), uintptr(unsafe.Pointer(&pix[0])),
uintptr(unsafe.Pointer(&bmi)), 0)
if n == 0 {
return 0
}
for i, p := range pix {
a := p >> 24
cr, cg, cb := p>>16&0xFF, p>>8&0xFF, p&0xFF
y := (cr*299 + cg*587 + cb*114) / 1000 // works on premultiplied pixels too
pix[i] = a<<24 | y<<16 | y<<8 | y
}
var bits unsafe.Pointer
hb, _, _ := pCreateDIBSection.Call(hdc, uintptr(unsafe.Pointer(&bmi)), 0, uintptr(unsafe.Pointer(&bits)), 0, 0)
if hb == 0 || bits == nil {
return 0
}
copy(unsafe.Slice((*uint32)(bits), w*h), pix)
ni := iconInfo{FIcon: 1, Mask: ii.Mask, Color: hb}
nh, _, _ := pCreateIconIndirect.Call(uintptr(unsafe.Pointer(&ni)))
pDeleteObject.Call(hb)
return nh
}
// styleColor applies the style to a color MirrorBar draws itself.
func styleColor(r, g, b int) (int, int, int) {
if iconMode == modeGray {
y := (r*299 + g*587 + b*114) / 1000
return y, y, y
}
return r, g, b
}
func addStyleMenu(menu uintptr) {
sub, _, _ := pCreatePopupMenu.Call()
for i, name := range styleNames {
var flags uintptr
if i == iconMode {
flags = mfChecked
}
pAppendMenu.Call(sub, flags, uintptr(menuStyleBase+i), uintptr(unsafe.Pointer(u16(name))))
}
pAppendMenu.Call(menu, mfPopup, sub, uintptr(unsafe.Pointer(u16("Icon style"))))
}
func handleStyleCommand(cmd uintptr) bool {
if cmd < menuStyleBase || cmd >= menuStyleBase+uintptr(len(styleNames)) {
return false
}
iconMode = int(cmd - menuStyleBase)
saveIconMode()
styleCacheClear()
return true
}