github.com/jmigpin/editor@v1.6.0/util/uiutil/widget/theme.go (about) 1 package widget 2 3 import ( 4 "image/color" 5 6 "github.com/jmigpin/editor/util/fontutil" 7 "github.com/jmigpin/editor/util/imageutil" 8 ) 9 10 //---------- 11 12 var DefaultPalette = Palette{ 13 "text_cursor_fg": nil, // present but nil uses the current fg 14 "text_fg": cint(0x0), 15 "text_bg": cint(0xffffff), 16 "text_selection_fg": nil, 17 "text_selection_bg": cint(0xeeee9e), // yellow 18 "text_colorize_string_fg": cint(0x008b00), // green 19 "text_colorize_string_bg": nil, 20 "text_colorize_comments_fg": cint(0x757575), // grey 600 21 "text_colorize_comments_bg": nil, 22 "text_highlightword_fg": nil, 23 "text_highlightword_bg": cint(0xc6ee9e), // green 24 "text_wrapline_fg": cint(0x0), 25 "text_wrapline_bg": cint(0xd8d8d8), 26 "text_parenthesis_fg": cint(0x0), 27 "text_parenthesis_bg": cint(0xc3c3c3), 28 "text_annotations_fg": cint(0x0), 29 "text_annotations_bg": cint(0xb0e0ef), 30 "text_annotations_select_fg": cint(0x0), 31 "text_annotations_select_bg": cint(0xefc7b0), 32 33 "scrollbar_bg": cint(0xf2f2f2), 34 "scrollhandle_normal": cint(0xb2b2b2), 35 "scrollhandle_hover": cint(0x8e8e8e), 36 "scrollhandle_select": cint(0x5f5f5f), 37 38 "button_hover_fg": nil, 39 "button_hover_bg": cint(0xdddddd), 40 "button_down_fg": nil, 41 "button_down_bg": cint(0xaaaaaa), 42 "button_sticky_fg": cint(0xffffff), 43 "button_sticky_bg": cint(0x0), 44 45 "pad": cint(0x8080ff), // helpful color to debug 46 "border": cint(0x00ff00), // helpful color to debug 47 "rect": cint(0xff8000), // helpful color to debug 48 } 49 50 //---------- 51 52 type Theme struct { 53 FontFace *fontutil.FontFace 54 Palette Palette 55 PaletteNamePrefix string 56 } 57 58 func (t *Theme) empty() bool { 59 return (t.FontFace == nil && 60 (t.Palette == nil || t.Palette.Empty()) && 61 t.PaletteNamePrefix == "") 62 } 63 64 func (t *Theme) Clear() { 65 if t.empty() { 66 *t = Theme{} 67 } 68 } 69 70 //---------- 71 72 // Can be set to nil to erase. 73 func (t *Theme) SetFontFace(ff *fontutil.FontFace) { 74 t.FontFace = ff 75 t.Clear() 76 } 77 78 // Can be set to nil to erase. 79 func (t *Theme) SetPalette(p Palette) { 80 t.Palette = p 81 t.Clear() 82 } 83 84 // Can be set to nil to erase. 85 func (t *Theme) SetPaletteColor(name string, c color.Color) { 86 // delete color 87 if c == nil { 88 if t.Palette != nil { 89 delete(t.Palette, name) 90 } 91 t.Clear() 92 return 93 } 94 95 if t.Palette == nil { 96 t.Palette = Palette{} 97 } 98 t.Palette[name] = c 99 } 100 101 // Can be set to "" to erase. 102 func (t *Theme) SetPaletteNamePrefix(prefix string) { 103 t.PaletteNamePrefix = prefix 104 t.Clear() 105 } 106 107 //---------- 108 109 type Palette map[string]color.Color 110 111 func (pal Palette) Empty() bool { 112 return pal == nil || len(pal) == 0 113 } 114 115 func (pal Palette) Merge(p2 Palette) { 116 for k, v := range p2 { 117 pal[k] = v 118 } 119 } 120 121 //---------- 122 123 func cint(c int) color.RGBA { 124 return imageutil.RgbaFromInt(c) 125 }