Repository: JanDeDobbeleer/oh-my-posh
Pull request #2347 (2022-05-28 22:29:34)

fix(accent): get correct accent color on Windows
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

### Description

<!--TemplateBody-->

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/cli/get.go b/src/cli/get.go
index 1d69a6f8..28a95c59 100644
--- a/src/cli/get.go
+++ b/src/cli/get.go
@@ -43,7 +43,7 @@ This command is used to get the value of the following variables:
 		case "shell":
 			fmt.Println(env.Shell())
 		case "accent":
-			rgb, err := color.GetAccentColor()
+			rgb, err := color.GetAccentColor(env)
 			if err != nil {
 				fmt.Println("error getting accent color:", err.Error())
 				return
diff --git a/src/color/colors.go b/src/color/colors.go
index d9776d8e..fff74fdb 100644
--- a/src/color/colors.go
+++ b/src/color/colors.go
@@ -2,13 +2,14 @@ package color
 
 import (
 	"fmt"
+	"oh-my-posh/environment"
 
 	"github.com/gookit/color"
 )
 
-func MakeColors(palette Palette, cacheEnabled bool, accentColor string) (colors AnsiColors) {
+func MakeColors(palette Palette, cacheEnabled bool, accentColor string, env environment.Environment) (colors AnsiColors) {
 	defaultColors := &DefaultColors{}
-	defaultColors.SetAccentColor(accentColor)
+	defaultColors.SetAccentColor(env, accentColor)
 	colors = defaultColors
 	if palette != nil {
 		colors = &PaletteColors{ansiColors: colors, palette: palette}
diff --git a/src/color/colors_unix.go b/src/color/colors_unix.go
index ebfdf072..1e40dd5d 100644
--- a/src/color/colors_unix.go
+++ b/src/color/colors_unix.go
@@ -2,13 +2,16 @@
 
 package color
 
-import "errors"
+import (
+	"errors"
+	"oh-my-posh/environment"
+)
 
-func GetAccentColor() (*RGB, error) {
+func GetAccentColor(env environment.Environment) (*RGB, error) {
 	return nil, errors.New("not implemented")
 }
 
-func (d *DefaultColors) SetAccentColor(defaultColor string) {
+func (d *DefaultColors) SetAccentColor(env environment.Environment, defaultColor string) {
 	if len(defaultColor) == 0 {
 		return
 	}
diff --git a/src/color/colors_windows.go b/src/color/colors_windows.go
index 2c748290..baa152c7 100644
--- a/src/color/colors_windows.go
+++ b/src/color/colors_windows.go
@@ -2,36 +2,29 @@ package color
 
 import (
 	"errors"
-	"syscall"
-	"unsafe"
+	"oh-my-posh/environment"
 
 	"github.com/gookit/color"
-	"golang.org/x/sys/windows"
 )
 
-var (
-	dwmapi                      = syscall.NewLazyDLL("dwmapi.dll")
-	procDwmGetColorizationColor = dwmapi.NewProc("DwmGetColorizationColor")
-)
-
-func GetAccentColor() (*RGB, error) {
-	var accentColor uint32
-	var pfOpaqueBlend bool
-	_, _, e := procDwmGetColorizationColor.Call(
-		uintptr(unsafe.Pointer(&accentColor)),
-		uintptr(unsafe.Pointer(&pfOpaqueBlend)))
-	if e != windows.ERROR_SUCCESS {
-		return nil, errors.New("unable to get accent color")
+func GetAccentColor(env environment.Environment) (*RGB, error) {
+	if env == nil {
+		return nil, errors.New("unable to get color without environment")
+	}
+	// see https://stackoverflow.com/questions/3560890/vista-7-how-to-get-glass-color
+	value, err := env.WindowsRegistryKeyValue(`HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM\ColorizationColor`)
+	if err != nil {
+		return nil, err
 	}
 	return &RGB{
-		R: byte(accentColor >> 16),
-		G: byte(accentColor >> 8),
-		B: byte(accentColor),
+		R: byte(value.Dword >> 16),
+		G: byte(value.Dword >> 8),
+		B: byte(value.Dword),
 	}, nil
 }
 
-func (d *DefaultColors) SetAccentColor(defaultColor string) {
-	rgb, err := GetAccentColor()
+func (d *DefaultColors) SetAccentColor(env environment.Environment, defaultColor string) {
+	rgb, err := GetAccentColor(env)
 	if err != nil {
 		d.accent = &Color{
 			Foreground: string(d.AnsiColorFromString(defaultColor, false)),
diff --git a/src/engine/config.go b/src/engine/config.go
index b5028103..76275073 100644
--- a/src/engine/config.go
+++ b/src/engine/config.go
@@ -57,7 +57,7 @@ type Config struct {
 // environment and configuration.
 func (cfg *Config) MakeColors(env environment.Environment) color.AnsiColors {
 	cacheDisabled := env.Getenv("OMP_CACHE_DISABLED") == "1"
-	return color.MakeColors(cfg.Palette, !cacheDisabled, cfg.AccentColor)
+	return color.MakeColors(cfg.Palette, !cacheDisabled, cfg.AccentColor, env)
 }
 
 func (cfg *Config) print(message string) {


Test patch:
diff --git a/src/color/colors_test.go b/src/color/colors_test.go
index 7fdfc9d9..1efd6bb7 100644
--- a/src/color/colors_test.go
+++ b/src/color/colors_test.go
@@ -30,18 +30,18 @@ func TestGetAnsiFromColorString(t *testing.T) {
 }
 
 func TestMakeColors(t *testing.T) {
-	colors := MakeColors(nil, false, "")
+	colors := MakeColors(nil, false, "", nil)
 	assert.IsType(t, &DefaultColors{}, colors)
 
-	colors = MakeColors(nil, true, "")
+	colors = MakeColors(nil, true, "", nil)
 	assert.IsType(t, &CachedColors{}, colors)
 	assert.IsType(t, &DefaultColors{}, colors.(*CachedColors).ansiColors)
 
-	colors = MakeColors(testPalette, false, "")
+	colors = MakeColors(testPalette, false, "", nil)
 	assert.IsType(t, &PaletteColors{}, colors)
 	assert.IsType(t, &DefaultColors{}, colors.(*PaletteColors).ansiColors)
 
-	colors = MakeColors(testPalette, true, "")
+	colors = MakeColors(testPalette, true, "", nil)
 	assert.IsType(t, &CachedColors{}, colors)
 	assert.IsType(t, &PaletteColors{}, colors.(*CachedColors).ansiColors)
 	assert.IsType(t, &DefaultColors{}, colors.(*CachedColors).ansiColors.(*PaletteColors).ansiColors)