summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/screenshot/theme.go
blob: a2bd5f6ffcf031eae0c74c29279556c8cb9f9042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package screenshot

import (
	"encoding/json"
	"os"
	"path/filepath"
	"strings"

	"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
	"github.com/AvengeMedia/DankMaterialShell/core/internal/utils"
)

type ThemeColors struct {
	Background string `json:"surface"`
	OnSurface  string `json:"on_surface"`
	Primary    string `json:"primary"`
}

type ColorScheme struct {
	Dark  ThemeColors `json:"dark"`
	Light ThemeColors `json:"light"`
}

type ColorsFile struct {
	Colors ColorScheme `json:"colors"`
}

var cachedStyle *OverlayStyle

func LoadOverlayStyle() OverlayStyle {
	if cachedStyle != nil {
		return *cachedStyle
	}

	style := DefaultOverlayStyle
	colors := loadColorsFile()
	if colors == nil {
		cachedStyle = &style
		return style
	}

	theme := &colors.Dark
	if isLightMode() {
		theme = &colors.Light
	}

	if bg, ok := parseHexColor(theme.Background); ok {
		style.BackgroundR, style.BackgroundG, style.BackgroundB = bg[0], bg[1], bg[2]
	}
	if text, ok := parseHexColor(theme.OnSurface); ok {
		style.TextR, style.TextG, style.TextB = text[0], text[1], text[2]
	}
	if accent, ok := parseHexColor(theme.Primary); ok {
		style.AccentR, style.AccentG, style.AccentB = accent[0], accent[1], accent[2]
	}

	cachedStyle = &style
	return style
}

func loadColorsFile() *ColorScheme {
	path := getColorsFilePath()
	data, err := os.ReadFile(path)
	if err != nil {
		return nil
	}

	var file ColorsFile
	if err := json.Unmarshal(data, &file); err != nil {
		return nil
	}

	return &file.Colors
}

func getColorsFilePath() string {
	cacheDir, err := os.UserCacheDir()
	if err != nil {
		log.Error("Failed to get user cache dir", "err", err)
		return ""
	}
	return filepath.Join(cacheDir, "DankMaterialShell", "dms-colors.json")
}

func isLightMode() bool {
	scheme, err := utils.GsettingsGet("org.gnome.desktop.interface", "color-scheme")
	if err != nil {
		return false
	}

	switch scheme {
	case "'prefer-light'", "'default'":
		return true
	}
	return false
}

func parseHexColor(hex string) ([3]uint8, bool) {
	hex = strings.TrimPrefix(hex, "#")
	if len(hex) != 6 {
		return [3]uint8{}, false
	}

	var r, g, b uint8
	for i, ptr := range []*uint8{&r, &g, &b} {
		val := 0
		for j := 0; j < 2; j++ {
			c := hex[i*2+j]
			val *= 16
			switch {
			case c >= '0' && c <= '9':
				val += int(c - '0')
			case c >= 'a' && c <= 'f':
				val += int(c - 'a' + 10)
			case c >= 'A' && c <= 'F':
				val += int(c - 'A' + 10)
			default:
				return [3]uint8{}, false
			}
		}
		*ptr = uint8(val)
	}

	return [3]uint8{r, g, b}, true
}