| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package theme |
| 7 |
|
| 8 |
import ( |
| 9 |
"image" |
| 10 |
"image/color" |
| 11 |
|
| 12 |
"golang.org/x/exp/shiny/unit" |
| 13 |
"golang.org/x/image/font" |
| 14 |
"golang.org/x/image/font/inconsolata" |
| 15 |
"golang.org/x/image/math/fixed" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
type FontFaceOptions struct { |
| 26 |
Style font.Style |
| 27 |
Weight font.Weight |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type FontFaceCatalog interface { |
| 40 |
AcquireFontFace(FontFaceOptions) font.Face |
| 41 |
ReleaseFontFace(FontFaceOptions, font.Face) |
| 42 |
|
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
type Color interface { |
| 51 |
Color(*Theme) color.Color |
| 52 |
Uniform(*Theme) *image.Uniform |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func StaticColor(c color.Color) Color { return staticColor{image.Uniform{c}} } |
| 57 |
|
| 58 |
type staticColor struct { |
| 59 |
u image.Uniform |
| 60 |
} |
| 61 |
|
| 62 |
func (s staticColor) Color(*Theme) color.Color { return s.u.C } |
| 63 |
func (s staticColor) Uniform(*Theme) *image.Uniform { return &s.u } |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
type Palette [PaletteLen]image.Uniform |
| 71 |
|
| 72 |
func (p *Palette) Light() *image.Uniform { return &p[Light] } |
| 73 |
func (p *Palette) Neutral() *image.Uniform { return &p[Neutral] } |
| 74 |
func (p *Palette) Dark() *image.Uniform { return &p[Dark] } |
| 75 |
func (p *Palette) Accent() *image.Uniform { return &p[Accent] } |
| 76 |
func (p *Palette) Foreground() *image.Uniform { return &p[Foreground] } |
| 77 |
func (p *Palette) Background() *image.Uniform { return &p[Background] } |
| 78 |
|
| 79 |
|
| 80 |
type PaletteIndex int |
| 81 |
|
| 82 |
func (i PaletteIndex) Color(t *Theme) color.Color { return t.GetPalette()[i].C } |
| 83 |
func (i PaletteIndex) Uniform(t *Theme) *image.Uniform { return &t.GetPalette()[i] } |
| 84 |
|
| 85 |
const ( |
| 86 |
|
| 87 |
|
| 88 |
Light = PaletteIndex(0) |
| 89 |
Neutral = PaletteIndex(1) |
| 90 |
Dark = PaletteIndex(2) |
| 91 |
|
| 92 |
|
| 93 |
Accent = PaletteIndex(3) |
| 94 |
|
| 95 |
|
| 96 |
Foreground = PaletteIndex(4) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
Background = PaletteIndex(5) |
| 101 |
|
| 102 |
PaletteLen = 6 |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
const DefaultDPI = 72.0 |
| 108 |
|
| 109 |
var ( |
| 110 |
|
| 111 |
DefaultFontFaceCatalog FontFaceCatalog = defaultFontFaceCatalog{} |
| 112 |
|
| 113 |
|
| 114 |
DefaultPalette = Palette{ |
| 115 |
Light: image.Uniform{C: color.RGBA{0xf5, 0xf5, 0xf5, 0xff}}, |
| 116 |
Neutral: image.Uniform{C: color.RGBA{0xee, 0xee, 0xee, 0xff}}, |
| 117 |
Dark: image.Uniform{C: color.RGBA{0xe0, 0xe0, 0xe0, 0xff}}, |
| 118 |
Accent: image.Uniform{C: color.RGBA{0x21, 0x96, 0xf3, 0xff}}, |
| 119 |
Foreground: image.Uniform{C: color.RGBA{0x00, 0x00, 0x00, 0xff}}, |
| 120 |
Background: image.Uniform{C: color.RGBA{0xff, 0xff, 0xff, 0xff}}, |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
Default *Theme |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
type defaultFontFaceCatalog struct{} |
| 134 |
|
| 135 |
func (defaultFontFaceCatalog) AcquireFontFace(FontFaceOptions) font.Face { |
| 136 |
return inconsolata.Regular8x16 |
| 137 |
} |
| 138 |
|
| 139 |
func (defaultFontFaceCatalog) ReleaseFontFace(FontFaceOptions, font.Face) {} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
type Theme struct { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
DPI float64 |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
FontFaceCatalog FontFaceCatalog |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
Palette *Palette |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
func (t *Theme) GetDPI() float64 { |
| 163 |
if t != nil && t.DPI != 0 { |
| 164 |
return t.DPI |
| 165 |
} |
| 166 |
return DefaultDPI |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
func (t *Theme) GetFontFaceCatalog() FontFaceCatalog { |
| 172 |
if t != nil && t.FontFaceCatalog != nil { |
| 173 |
return t.FontFaceCatalog |
| 174 |
} |
| 175 |
return DefaultFontFaceCatalog |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (t *Theme) GetPalette() *Palette { |
| 181 |
if t != nil && t.Palette != nil { |
| 182 |
return t.Palette |
| 183 |
} |
| 184 |
return &DefaultPalette |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
func (t *Theme) AcquireFontFace(o FontFaceOptions) font.Face { |
| 189 |
return t.GetFontFaceCatalog().AcquireFontFace(o) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func (t *Theme) ReleaseFontFace(o FontFaceOptions, f font.Face) { |
| 194 |
t.GetFontFaceCatalog().ReleaseFontFace(o, f) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
func (t *Theme) Pixels(v unit.Value) fixed.Int26_6 { |
| 199 |
c := t.Convert(v, unit.Px) |
| 200 |
return fixed.Int26_6(c.F * 64) |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
func (t *Theme) Convert(v unit.Value, to unit.Unit) unit.Value { |
| 205 |
if v.U == to { |
| 206 |
return v |
| 207 |
} |
| 208 |
return unit.Value{ |
| 209 |
F: v.F * t.pixelsPer(v.U) / t.pixelsPer(to), |
| 210 |
U: to, |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
func (t *Theme) pixelsPer(u unit.Unit) float64 { |
| 216 |
switch u { |
| 217 |
case unit.Px: |
| 218 |
return 1 |
| 219 |
case unit.Dp: |
| 220 |
return t.GetDPI() / unit.DensityIndependentPixelsPerInch |
| 221 |
case unit.Pt: |
| 222 |
return t.GetDPI() / unit.PointsPerInch |
| 223 |
case unit.Mm: |
| 224 |
return t.GetDPI() / unit.MillimetresPerInch |
| 225 |
case unit.In: |
| 226 |
return t.GetDPI() |
| 227 |
} |
| 228 |
|
| 229 |
f := t.AcquireFontFace(FontFaceOptions{}) |
| 230 |
defer t.ReleaseFontFace(FontFaceOptions{}, f) |
| 231 |
|
| 232 |
|
| 233 |
h := float64(f.Metrics().Height) / 64 |
| 234 |
switch u { |
| 235 |
case unit.Em: |
| 236 |
return h |
| 237 |
case unit.Ex: |
| 238 |
return h / 2 |
| 239 |
case unit.Ch: |
| 240 |
if advance, ok := f.GlyphAdvance('0'); ok { |
| 241 |
return float64(advance) / 64 |
| 242 |
} |
| 243 |
return h / 2 |
| 244 |
} |
| 245 |
return 1 |
| 246 |
} |