This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/default/theme/default.go

259 lines
8.1 KiB
Go
Raw Normal View History

2023-02-02 22:57:18 -07:00
package theme
import "image"
2023-02-27 10:48:44 -07:00
import "bytes"
import _ "embed"
import _ "image/png"
2023-02-26 20:20:17 -07:00
import "image/color"
2023-02-02 22:57:18 -07:00
import "golang.org/x/image/font"
2023-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo"
2023-03-04 14:18:43 -07:00
import "git.tebibyte.media/sashakoshka/tomo/data"
2023-02-02 22:57:18 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-04-30 11:47:36 -06:00
import "git.tebibyte.media/sashakoshka/tomo/artist/artutil"
2023-03-31 11:45:52 -06:00
import defaultfont "git.tebibyte.media/sashakoshka/tomo/default/font"
import "git.tebibyte.media/sashakoshka/tomo/artist/patterns"
2023-02-02 22:57:18 -07:00
2023-05-02 21:19:43 -06:00
//go:embed assets/default.png
2023-02-27 10:48:44 -07:00
var defaultAtlasBytes []byte
2023-04-30 11:47:36 -06:00
var defaultAtlas artist.Canvas
2023-05-02 21:19:43 -06:00
var defaultTextures [7][7]artist.Pattern
2023-03-04 20:07:59 -07:00
//go:embed assets/wintergreen-icons-small.png
var defaultIconsSmallAtlasBytes []byte
var defaultIconsSmall [640]binaryIcon
2023-03-10 16:53:27 -07:00
//go:embed assets/wintergreen-icons-large.png
var defaultIconsLargeAtlasBytes []byte
var defaultIconsLarge [640]binaryIcon
2023-02-27 10:48:44 -07:00
func atlasCell (col, row int, border artist.Inset) {
2023-05-02 21:19:43 -06:00
bounds := image.Rect(0, 0, 8, 8).Add(image.Pt(col, row).Mul(8))
2023-02-27 10:48:44 -07:00
defaultTextures[col][row] = patterns.Border {
2023-04-30 11:47:36 -06:00
Canvas: artist.Cut(defaultAtlas, bounds),
2023-02-27 10:48:44 -07:00
Inset: border,
}
}
func atlasCol (col int, border artist.Inset) {
for index, _ := range defaultTextures[col] {
atlasCell(col, index, border)
}
}
2023-03-04 20:07:59 -07:00
type binaryIcon struct {
data []bool
stride int
}
2023-04-30 11:47:36 -06:00
func (icon binaryIcon) Draw (destination artist.Canvas, color color.RGBA, at image.Point) {
2023-03-04 20:07:59 -07:00
bounds := icon.Bounds().Add(at).Intersect(destination.Bounds())
point := image.Point { }
data, stride := destination.Buffer()
for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ {
for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ {
srcPoint := point.Sub(at)
srcIndex := srcPoint.X + srcPoint.Y * icon.stride
dstIndex := point.X + point.Y * stride
if icon.data[srcIndex] {
data[dstIndex] = color
}
}}
}
func (icon binaryIcon) Bounds () image.Rectangle {
return image.Rect(0, 0, icon.stride, len(icon.data) / icon.stride)
}
func binaryIconFrom (source image.Image, clip image.Rectangle) (icon binaryIcon) {
bounds := source.Bounds().Intersect(clip)
if bounds.Empty() { return }
icon.stride = bounds.Dx()
icon.data = make([]bool, bounds.Dx() * bounds.Dy())
point := image.Point { }
dstIndex := 0
for point.Y = bounds.Min.Y; point.Y < bounds.Max.Y; point.Y ++ {
for point.X = bounds.Min.X; point.X < bounds.Max.X; point.X ++ {
r, g, b, a := source.At(point.X, point.Y).RGBA()
if a > 0x8000 && (r + g + b) / 3 < 0x8000 {
icon.data[dstIndex] = true
}
dstIndex ++
}}
return
}
2023-02-27 10:48:44 -07:00
func init () {
defaultAtlasImage, _, _ := image.Decode(bytes.NewReader(defaultAtlasBytes))
2023-04-30 11:47:36 -06:00
defaultAtlas = artist.FromImage(defaultAtlasImage)
2023-02-27 10:48:44 -07:00
2023-05-02 21:19:43 -06:00
atlasCol(0, artist.I(0))
atlasCol(1, artist.I(3))
atlasCol(2, artist.I(1))
atlasCol(3, artist.I(1))
atlasCol(4, artist.I(1))
atlasCol(5, artist.I(2))
atlasCol(6, artist.I(1))
2023-03-04 20:07:59 -07:00
// set up small icons
defaultIconsSmallAtlasImage, _, _ := image.Decode (
bytes.NewReader(defaultIconsSmallAtlasBytes))
point := image.Point { }
iconIndex := 0
2023-03-10 16:53:27 -07:00
for point.Y = 0; point.Y < 20; point.Y ++ {
for point.X = 0; point.X < 32; point.X ++ {
2023-03-04 20:07:59 -07:00
defaultIconsSmall[iconIndex] = binaryIconFrom (
defaultIconsSmallAtlasImage,
2023-03-10 16:53:27 -07:00
image.Rect(0, 0, 16, 16).Add(point.Mul(16)))
iconIndex ++
}}
// set up large icons
defaultIconsLargeAtlasImage, _, _ := image.Decode (
bytes.NewReader(defaultIconsLargeAtlasBytes))
point = image.Point { }
iconIndex = 0
for point.Y = 0; point.Y < 8; point.Y ++ {
for point.X = 0; point.X < 32; point.X ++ {
defaultIconsLarge[iconIndex] = binaryIconFrom (
defaultIconsLargeAtlasImage,
image.Rect(0, 0, 32, 32).Add(point.Mul(32)))
iconIndex ++
}}
iconIndex = 384
for point.Y = 8; point.Y < 12; point.Y ++ {
for point.X = 0; point.X < 32; point.X ++ {
defaultIconsLarge[iconIndex] = binaryIconFrom (
defaultIconsLargeAtlasImage,
image.Rect(0, 0, 32, 32).Add(point.Mul(32)))
2023-03-04 20:07:59 -07:00
iconIndex ++
}}
2023-02-27 10:48:44 -07:00
}
2023-02-02 22:57:18 -07:00
// Default is the default theme.
type Default struct { }
// FontFace returns the default font face.
2023-03-30 23:06:29 -06:00
func (Default) FontFace (style tomo.FontStyle, size tomo.FontSize, c tomo.Case) font.Face {
2023-02-02 22:57:18 -07:00
switch style {
2023-03-30 23:06:29 -06:00
case tomo.FontStyleBold:
2023-02-02 22:57:18 -07:00
return defaultfont.FaceBold
2023-03-30 23:06:29 -06:00
case tomo.FontStyleItalic:
2023-02-02 22:57:18 -07:00
return defaultfont.FaceItalic
2023-03-30 23:06:29 -06:00
case tomo.FontStyleBoldItalic:
2023-02-02 22:57:18 -07:00
return defaultfont.FaceBoldItalic
default:
return defaultfont.FaceRegular
}
}
// Icon returns an icon from the default set corresponding to the given name.
2023-03-30 23:06:29 -06:00
func (Default) Icon (id tomo.Icon, size tomo.IconSize, c tomo.Case) artist.Icon {
if size == tomo.IconSizeLarge {
2023-03-10 16:53:27 -07:00
if id < 0 || int(id) >= len(defaultIconsLarge) {
return nil
} else {
return defaultIconsLarge[id]
}
2023-03-04 20:07:59 -07:00
} else {
if id < 0 || int(id) >= len(defaultIconsSmall) {
return nil
} else {
return defaultIconsSmall[id]
}
}
2023-02-02 22:57:18 -07:00
}
2023-03-04 14:18:43 -07:00
// MimeIcon returns an icon from the default set corresponding to the given mime.
// type.
2023-03-30 23:06:29 -06:00
func (Default) MimeIcon (data.Mime, tomo.IconSize, tomo.Case) artist.Icon {
2023-03-04 14:18:43 -07:00
// TODO
return nil
}
2023-02-02 22:57:18 -07:00
// Pattern returns a pattern from the default theme corresponding to the given
// pattern ID.
2023-03-30 23:06:29 -06:00
func (Default) Pattern (id tomo.Pattern, state tomo.State, c tomo.Case) artist.Pattern {
2023-02-27 10:48:44 -07:00
offset := 0; switch {
2023-04-02 17:01:06 -06:00
case state.Disabled: offset = 1
case state.Pressed && state.On: offset = 4
case state.Focused && state.On: offset = 6
case state.On: offset = 2
case state.Pressed: offset = 3
case state.Focused: offset = 5
2023-02-27 10:48:44 -07:00
}
2023-02-26 20:20:17 -07:00
switch id {
2023-03-30 23:06:29 -06:00
case tomo.PatternBackground: return patterns.Uhex(0xaaaaaaFF)
case tomo.PatternDead: return defaultTextures[0][offset]
case tomo.PatternRaised: return defaultTextures[1][offset]
case tomo.PatternSunken: return defaultTextures[2][offset]
case tomo.PatternPinboard: return defaultTextures[3][offset]
2023-05-02 21:19:43 -06:00
case tomo.PatternButton: return defaultTextures[1][offset]
case tomo.PatternInput: return defaultTextures[2][offset]
case tomo.PatternGutter: return defaultTextures[2][offset]
case tomo.PatternHandle: return defaultTextures[3][offset]
case tomo.PatternLine: return defaultTextures[0][offset]
case tomo.PatternMercury: return defaultTextures[4][offset]
case tomo.PatternTableHead: return defaultTextures[5][offset]
case tomo.PatternTableCell: return defaultTextures[5][offset]
case tomo.PatternLamp: return defaultTextures[6][offset]
default: return patterns.Uhex(0xFF00FFFF)
2023-02-02 22:57:18 -07:00
}
}
2023-03-30 23:06:29 -06:00
func (Default) Color (id tomo.Color, state tomo.State, c tomo.Case) color.RGBA {
2023-04-30 11:47:36 -06:00
if state.Disabled { return artutil.Hex(0x444444FF) }
2023-04-01 23:56:19 -06:00
2023-04-30 11:47:36 -06:00
return artutil.Hex (map[tomo.Color] uint32 {
2023-04-20 17:57:55 -06:00
tomo.ColorBlack: 0x272d24FF,
tomo.ColorRed: 0x8c4230FF,
tomo.ColorGreen: 0x69905fFF,
tomo.ColorYellow: 0x9a973dFF,
tomo.ColorBlue: 0x3d808fFF,
tomo.ColorPurple: 0x8c608bFF,
tomo.ColorCyan: 0x3d8f84FF,
tomo.ColorWhite: 0xaea894FF,
tomo.ColorBrightBlack: 0x4f5142FF,
tomo.ColorBrightRed: 0xbd6f59FF,
tomo.ColorBrightGreen: 0x8dad84FF,
tomo.ColorBrightYellow: 0xe2c558FF,
tomo.ColorBrightBlue: 0x77b1beFF,
tomo.ColorBrightPurple: 0xc991c8FF,
tomo.ColorBrightCyan: 0x74c7b7FF,
tomo.ColorBrightWhite: 0xcfd7d2FF,
2023-04-01 23:56:19 -06:00
tomo.ColorForeground: 0x000000FF,
2023-05-02 21:19:43 -06:00
tomo.ColorMidground: 0x656565FF,
2023-04-01 23:56:19 -06:00
tomo.ColorBackground: 0xAAAAAAFF,
2023-05-02 21:19:43 -06:00
tomo.ColorShadow: 0x000000FF,
tomo.ColorShine: 0xFFFFFFFF,
tomo.ColorAccent: 0xff3300FF,
2023-04-01 23:56:19 -06:00
} [id])
2023-02-26 20:20:17 -07:00
}
// Padding returns the default padding value for the given pattern.
2023-03-30 23:06:29 -06:00
func (Default) Padding (id tomo.Pattern, c tomo.Case) artist.Inset {
2023-02-27 15:00:28 -07:00
switch id {
2023-05-02 21:19:43 -06:00
case tomo.PatternGutter: return artist.I(0)
default: return artist.I(6)
2023-02-27 15:00:28 -07:00
}
}
2023-02-09 09:38:01 -07:00
// Margin returns the default margin value for the given pattern.
2023-03-30 23:06:29 -06:00
func (Default) Margin (id tomo.Pattern, c tomo.Case) image.Point {
2023-05-02 21:19:43 -06:00
return image.Pt(6, 6)
2023-02-02 22:57:18 -07:00
}
// Hints returns rendering optimization hints for a particular pattern.
// These are optional, but following them may result in improved
// performance.
2023-03-30 23:06:29 -06:00
func (Default) Hints (pattern tomo.Pattern, c tomo.Case) (hints tomo.Hints) {
return
}
2023-02-02 22:57:18 -07:00
// Sink returns the default sink vector for the given pattern.
2023-03-30 23:06:29 -06:00
func (Default) Sink (pattern tomo.Pattern, c tomo.Case) image.Point {
2023-02-02 22:57:18 -07:00
return image.Point { 1, 1 }
}