diff --git a/README.md b/README.md index 57af495..c382295 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,13 @@ in headless tests (screenshot-hash regressions). `LoadGTKTheme(css)` parses libadwaita / GTK3 `@define-color` declarations into a `Theme`, so any GTK-desktop palette (Adwaita, Juno, WhiteSur, Solarized …) drives the widget ink colours. + `SlateLight(brand)` / `SlateDark(brand)` / `Slate(brand, dark)` is the Tailwind + slate look with one brand accent (modelled on the go-authn account page, whose + `#0079a8` it reproduces with the derived hover and ink): a slate-50 page, white + cards, soft borders and shadows. `Frame.Card` is the card (rounded surface, + shadow, heading over a hairline), `Chip.Style` the plain and brand pills, and + `GraphView.CornerRadius` the rounded nodes and clusters. The host reads the + system's light/dark preference and picks the variant. - **Pure Go + CGO=0**: no C toolchain, no shared libraries. Builds for every Go target including `GOOS=js GOARCH=wasm`. - **A11y bridge**: core widgets implement an opt-in `Accessible` diff --git a/chip.go b/chip.go index 6438176..d9ac730 100644 --- a/chip.go +++ b/chip.go @@ -33,6 +33,10 @@ type Chip struct { // (A == 0) draws no dot and reproduces the original layout exactly, // so the field is fully backward-compatible. Dot RGBA + // Style picks the look: ChipDefault (the zero value, the original + // rectangle), ChipPlain or ChipBrand (rounded pills). Set-once appearance + // config. + Style ChipStyle } // Chip sizing constants. PadX / PadY are the inner insets from the @@ -97,8 +101,19 @@ func (c *Chip) Draw(p painter.Painter, theme *Theme) { } c.SetBounds(r) } - fillRect(p, r.X, r.Y, r.W, r.H, theme.SurfaceAlt) - strokeRect(p, r.X, r.Y, r.W, r.H, theme.Border) + ink := theme.OnSurface + closeInk := theme.Border + switch c.Style { + case ChipPlain, ChipBrand: + // A pill: fully rounded, no outline, a quiet tinted fill. + bg := themeChipBg(theme, c.Style) + ink = themeChipInk(theme, c.Style) + closeInk = ink + fillRoundRect(p, r.X, r.Y, r.W, r.H, r.H/2, bg) + default: + fillRect(p, r.X, r.Y, r.W, r.H, theme.SurfaceAlt) + strokeRect(p, r.X, r.Y, r.W, r.H, theme.Border) + } tx := r.X + padX if hasDot { dotY := r.Y + (r.H-dotD)/2 @@ -106,11 +121,54 @@ func (c *Chip) Draw(p painter.Painter, theme *Theme) { tx += dotD + dotGap } ty := r.Y + (r.H-c.glyphHeight())/2 - c.drawText(p, tx, ty, c.Text, theme.OnSurface) + c.drawText(p, tx, ty, c.Text, ink) if c.Closable { cx := r.X + r.W - padX - closeW + (closeW-c.textWidth("x"))/2 - c.drawText(p, cx, ty, "x", theme.Border) + c.drawText(p, cx, ty, "x", closeInk) + } +} + +// ChipStyle is how a Chip is painted. +type ChipStyle int + +const ( + // ChipDefault is the original chip: a SurfaceAlt rectangle with a Border + // outline. + ChipDefault ChipStyle = iota + // ChipPlain is a quiet pill: fully rounded, a neutral tint, no outline + // (Tailwind's chip-plain). A theme's Extra["chip_plain_bg"] and + // ["chip_plain_ink"] set its colours; without them it is SurfaceAlt and + // OnSurface. + ChipPlain + // ChipBrand is the same pill in the brand colour: the brand at 10% for the + // fill and the brand-as-text for the ink (Extra["chip_brand_bg"] and + // ["brand_ink"]; without them the Accent over the Surface). + ChipBrand +) + +// themeChipBg is the fill of a pill chip of style s. +func themeChipBg(theme *Theme, s ChipStyle) RGBA { + if s == ChipBrand { + if c, ok := theme.Extra["chip_brand_bg"]; ok { + return c + } + return blendRGBA(theme.Accent, theme.Surface, 0.10) + } + if c, ok := theme.Extra["chip_plain_bg"]; ok { + return c + } + return theme.SurfaceAlt +} + +// themeChipInk is the text of a pill chip of style s. +func themeChipInk(theme *Theme, s ChipStyle) RGBA { + if s == ChipBrand { + return themeInk(theme) + } + if c, ok := theme.Extra["chip_plain_ink"]; ok { + return c } + return theme.OnSurface } // OnEvent fires OnClose when an EventClick lands in the right-hand diff --git a/graphview.go b/graphview.go index 0e9ec49..3eacf73 100644 --- a/graphview.go +++ b/graphview.go @@ -99,6 +99,10 @@ type GraphView struct { // host whose labels are longer than a few characters sets it once, before // the first draw; a node's label is clipped to its box. NodeWidth int + // CornerRadius rounds the node pills and, by half as much again, the + // cluster boxes, in logical pixels (default 0: square, as before). A + // set-once appearance config, like NodeWidth. + CornerRadius int // ShowLegend draws the category legend along the bottom edge. ShowLegend bool // OnActivate is called with the selection on Enter or a double selection. @@ -172,6 +176,27 @@ const ( graphHitSlack = 3 ) +// clusterRadius is the rounding of a cluster box: half again the node pill's. +func (g *GraphView) clusterRadius() int { return g.CornerRadius * 3 / 2 } + +// fillBox fills r, rounded by radius when radius > 0, square otherwise. +func (g *GraphView) fillBox(p painter.Painter, r Rect, radius int, c RGBA) { + if radius > 0 { + fillRoundRect(p, r.X, r.Y, r.W, r.H, scaled(radius), c) + return + } + fillRect(p, r.X, r.Y, r.W, r.H, c) +} + +// strokeBox outlines r, rounded by radius when radius > 0, square otherwise. +func (g *GraphView) strokeBox(p painter.Painter, r Rect, radius int, c RGBA) { + if radius > 0 { + strokeRoundRect(p, r.X, r.Y, r.W, r.H, scaled(radius), c) + return + } + strokeRect(p, r.X, r.Y, r.W, r.H, c) +} + // nodeW is the width of a node box. func (g *GraphView) nodeW() int { if g.NodeWidth > 0 { @@ -404,8 +429,8 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) { visible := map[string]bool{} withClip(p, r, func() { for id, box := range g.clusterBox { - fillRect(p, box.X, box.Y, box.W, box.H, theme.Surface) - strokeRect(p, box.X, box.Y, box.W, box.H, theme.Border) + g.fillBox(p, box, g.clusterRadius(), theme.Surface) + g.strokeBox(p, box, g.clusterRadius(), theme.Border) g.drawText(p, box.X+graphGap, box.Y+2, labels[id], theme.OnSurface) } nodes := g.Model.Nodes.Slice() @@ -426,7 +451,7 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) { if !visible[n.ID] { col = blendRGBA(col, theme.Background, 0.25) } - fillRect(p, box.X, box.Y, box.W, box.H, col) + g.fillBox(p, box, g.CornerRadius, col) // Label ink by the swatch's lightness, so it reads on every colour. ink := RGB(0x10, 0x10, 0x10) if luminance(col) < 0.4 { @@ -438,7 +463,7 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) { g.drawText(p, box.X+3, box.Y+(box.H-g.glyphHeight())/2, n.Label, ink) }) if sel == "node:"+n.ID { - strokeRect(p, box.X-2, box.Y-2, box.W+4, box.H+4, theme.Accent) + g.strokeBox(p, Rect{X: box.X - 2, Y: box.Y - 2, W: box.W + 4, H: box.H + 4}, g.CornerRadius+2, theme.Accent) } } if g.ShowLegend { diff --git a/layout.go b/layout.go index c999bcf..be52d21 100644 --- a/layout.go +++ b/layout.go @@ -682,6 +682,16 @@ type Frame struct { // on the bar toggles the collapsed state. It forces a title bar even when // Title is "". Collapsible bool + // Card turns the frame into a CARD: a rounded Theme.Surface with a soft + // Theme.Border outline and, when the theme has one (Extra["shadow"]), the + // soft two-layer card shadow, padded by Padding (default 12) with a title, + // if any, set as a heading over a hairline rather than as a filled band. It + // is the surface of a Tailwind-style page: Slate gives it its colours. The + // zero value keeps the plain border-only frame. Set-once appearance config, + // like Padding. + Card bool + // CornerRadius is a Card's corner rounding in logical pixels (default 14). + CornerRadius int // collapsed holds the reactive collapse state, MVVM-only, exposed via // [Frame.Collapsed]. Meaningful only when a title bar is present. @@ -704,6 +714,43 @@ func (f *Frame) Collapsed() *mvvm.Observable[bool] { // the Frame lays it out at scaled(FrameTitleH) device pixels. const FrameTitleH = 22 +// Card-style frame metrics, in logical pixels. +const ( + frameCardPadding = 12 + frameCardRadius = 14 +) + +// cardRadius is the corner rounding of a Card frame. +func (f *Frame) cardRadius() int { + if f.CornerRadius > 0 { + return scaled(f.CornerRadius) + } + return scaled(frameCardRadius) +} + +// drawCard paints a Card frame's ground: the shadow (when the theme has one), +// the rounded Theme.Surface, and the soft outline. +func (f *Frame) drawCard(p painter.Painter, theme *Theme, r Rect) { + rad := f.cardRadius() + if sh, ok := theme.Extra["shadow"]; ok && sh.A != 0 { + // Two soft layers, as CSS `0 1px 2px, 0 4px 16px -4px`: a tight one under + // the edge and a wide faint one below it. + fillRoundRect(p, r.X, r.Y+1, r.W, r.H, rad, RGBA{R: sh.R, G: sh.G, B: sh.B, A: sh.A / 2}) + fillRoundRect(p, r.X+scaled(4), r.Y+scaled(5), r.W-2*scaled(4), r.H, rad, RGBA{R: sh.R, G: sh.G, B: sh.B, A: sh.A / 2}) + } + fillRoundRect(p, r.X, r.Y, r.W, r.H, rad, theme.Surface) + strokeRoundRect(p, r.X, r.Y, r.W, r.H, rad, theme.Border) +} + +// drawCardTitle paints a Card's heading in the theme's ink over a hairline. +func (f *Frame) drawCardTitle(p painter.Painter, theme *Theme, r Rect, hh int) { + bw := strokeWidth() + pad := f.inset() + ty := r.Y + bw + scaled(frameCardPadding)/2 + (hh-f.glyphHeight())/2 + f.drawText(p, r.X+pad, ty, f.Title, theme.OnSurface) + fillRect(p, r.X+pad, r.Y+bw+scaled(frameCardPadding)/2+hh, r.W-2*pad, 1, theme.Border) +} + // NewFrame wraps child in a Frame. child may be nil (the Frame then // just draws its border + accepts no events). func NewFrame(child Widget) *Frame { return &Frame{child: child} } @@ -724,6 +771,9 @@ func (f *Frame) inset() int { pad := f.Padding if pad == 0 { pad = defaultPadding + if f.Card { + pad = frameCardPadding + } } if pad < 0 { pad = 0 @@ -802,6 +852,16 @@ func (f *Frame) drawTitleBar(p painter.Painter, theme *Theme, r Rect, hh int) { func (f *Frame) Draw(p painter.Painter, theme *Theme) { r := f.Bounds() hh := f.headerH() + if f.Card { + f.drawCard(p, theme, r) + if f.Title != "" { + f.drawCardTitle(p, theme, r, hh) + } + if f.child != nil { + f.child.Draw(p, theme) + } + return + } if f.Collapsed().Get() && hh > 0 { strokeRect(p, r.X, r.Y, r.W, hh+2*strokeWidth(), theme.Border) f.drawTitleBar(p, theme, r, hh) diff --git a/mvvm_gate_test.go b/mvvm_gate_test.go index 091c906..87ece30 100644 --- a/mvvm_gate_test.go +++ b/mvvm_gate_test.go @@ -54,7 +54,7 @@ var mvvmMigratedWidgets = map[string]map[string]bool{ "Expander": {"Label": true, "Content": true}, // Batch 3 (index/selection widgets): the current index / open / collapsed // state is unexported behind accessors; the slices + flags below are config. - "Frame": {"Padding": true, "Title": true, "Collapsible": true}, + "Frame": {"Padding": true, "Title": true, "Collapsible": true, "Card": true, "CornerRadius": true}, "ViewSwitcher": {"Views": true}, "CycleButton": {"Options": true}, "Pagination": {"Total": true}, @@ -298,7 +298,7 @@ var mvvmMigratedWidgets = map[string]map[string]bool{ "Card": {"Body": true, "Footer": true, "Title": true}, "CardMeta": {"Author": true, "Comments": true, "Score": true, "Time": true}, "ChatBubble": {"Sender": true, "Text": true}, - "Chip": {"Closable": true, "Dot": true, "Text": true}, + "Chip": {"Closable": true, "Dot": true, "Text": true, "Style": true}, "Container": {"Layout": true}, "DatabaseEditor": {"BarHeight": true, "EditorHeight": true, "ErrorHeight": true, "TreeWidth": true}, // Closable is a set-once capability flag (like Window.Closable); Input is the @@ -364,7 +364,7 @@ var mvvmMigratedWidgets = map[string]map[string]bool{ // (Splitter.Open), enabled is the button's Disabled Observable; the hover // timer is unexported. "ClosablePane": {"Title": true, "Body": true, "CloseLabel": true, "LastLabel": true, "Button": true}, - "GraphView": {"Model": true, "Palette": true, "MaxEdgeWidth": true, "ShowLegend": true, "NodeWidth": true}, + "GraphView": {"Model": true, "Palette": true, "MaxEdgeWidth": true, "ShowLegend": true, "NodeWidth": true, "CornerRadius": true}, } // TestMigratedWidgetsHaveNoImperativeState is the enforcement gate. It parses the diff --git a/slate_widgets_test.go b/slate_widgets_test.go new file mode 100644 index 0000000..101e283 --- /dev/null +++ b/slate_widgets_test.go @@ -0,0 +1,157 @@ +// Copyright (c) 2026 the go-widgets/toolkit authors. All rights reserved. +// Use of this source code is governed by a BSD-3-Clause license that can be +// found in the LICENSE file at the root of this repository. + +package toolkit + +import "testing" + +// A card Frame is a rounded surface: its corner shows the page behind it, its +// middle the card colour, and a light theme's shadow darkens the page below it. +func TestFrameCardIsARoundedSurfaceWithAShadow(t *testing.T) { + const w, h = 120, 90 + card := func(theme *Theme, title string) []byte { + f := NewFrame(NewLabel("body")) + f.Card, f.Title = true, title + f.SetBounds(Rect{X: 10, Y: 10, W: 100, H: 60}) + surf := make([]byte, w*h*4) + fillRect(newP(surf, w), 0, 0, w, h, theme.Background) + f.Draw(newP(surf, w), theme) + return surf + } + light := SlateLight(RGB(0, 0x79, 0xA8)) + s := card(light, "") + if got := pixelAt(s, w, 10, 10); got != light.Background { + t.Errorf("the corner pixel %+v must still be the page %+v (rounded)", got, light.Background) + } + if got := pixelAt(s, w, 60, 40); got != light.Surface { + t.Errorf("the middle %+v must be the card %+v", got, light.Surface) + } + if got := pixelAt(s, w, 60, 73); got == light.Background { + t.Error("the shadow below a light card left the page untouched") + } + d := SlateDark(RGB(0, 0x79, 0xA8)) + if got := pixelAt(card(d, ""), w, 60, 73); got != d.Background { + t.Errorf("a dark card has no shadow: %+v", got) + } + // A titled card sets its heading in the ink over a hairline, not a band. + titled := card(light, "Detail") + band := pixelAt(titled, w, 60, 18) + if band == light.SurfaceAlt { + t.Error("a card heading must not be a filled band") + } + hairline := false + for y := 20; y < 50; y++ { + if pixelAt(titled, w, 60, y) == light.Border { + hairline = true + } + } + if !hairline { + t.Error("no hairline under the heading") + } + // The plain frame is unchanged: square, border only. + plain := NewFrame(NewLabel("body")) + plain.SetBounds(Rect{X: 10, Y: 10, W: 100, H: 60}) + ps := make([]byte, w*h*4) + plain.Draw(newP(ps, w), light) + if pixelAt(ps, w, 10, 10) != light.Border { + t.Error("a plain frame's corner is its square border") + } +} + +func TestFrameCardPaddingAndRadius(t *testing.T) { + f := NewFrame(NewLabel("x")) + f.Card = true + f.SetBounds(Rect{X: 0, Y: 0, W: 100, H: 60}) + if b := f.child.Bounds(); b.X != 1+frameCardPadding || b.Y != 1+frameCardPadding { + t.Errorf("card padding: child at %v", b) + } + if f.cardRadius() != frameCardRadius { + t.Errorf("default radius %d", f.cardRadius()) + } + f.CornerRadius = 5 + if f.cardRadius() != 5 { + t.Errorf("radius %d", f.cardRadius()) + } + f.Padding = 3 // an explicit padding wins over the card default + f.SetBounds(Rect{X: 0, Y: 0, W: 100, H: 60}) + if f.child.Bounds().X != 1+3 { + t.Errorf("explicit padding: child at %v", f.child.Bounds()) + } +} + +func TestChipStyles(t *testing.T) { + const w, h = 90, 20 + draw := func(theme *Theme, st ChipStyle) []byte { + c := NewChip("ok") + c.Style = st + c.SetBounds(Rect{X: 2, Y: 2, W: 60, H: 16}) + surf := make([]byte, w*h*4) + fillRect(newP(surf, w), 0, 0, w, h, theme.Surface) + c.Draw(newP(surf, w), theme) + return surf + } + light := SlateLight(RGB(0, 0x79, 0xA8)) + // A pill: the corner is the ground, the middle the tint. + for _, tc := range []struct { + st ChipStyle + want RGBA + }{{ChipPlain, light.Extra["chip_plain_bg"]}, {ChipBrand, light.Extra["chip_brand_bg"]}} { + s := draw(light, tc.st) + if pixelAt(s, w, 2, 2) != light.Surface { + t.Errorf("style %d: the corner is not rounded", tc.st) + } + if got := pixelAt(s, w, 30, 4); got != tc.want { + t.Errorf("style %d: fill %+v, want %+v", tc.st, got, tc.want) + } + } + // The default chip is the original rectangle: its corner is its border. + if pixelAt(draw(light, ChipDefault), w, 2, 2) != light.Border { + t.Error("the default chip changed") + } + // A theme with no chip colours falls back to SurfaceAlt / OnSurface / Accent. + bare := DefaultLight() + if themeChipBg(bare, ChipPlain) != bare.SurfaceAlt || themeChipInk(bare, ChipPlain) != bare.OnSurface { + t.Error("plain chip fallback") + } + if themeChipBg(bare, ChipBrand) != blendRGBA(bare.Accent, bare.Surface, 0.10) || themeChipInk(bare, ChipBrand) != bare.Accent { + t.Error("brand chip fallback") + } + // A closable pill draws its x in the chip's ink. + cc := NewClosableChip("ok", nil) + cc.Style = ChipBrand + cc.SetBounds(Rect{X: 2, Y: 2, W: 70, H: 16}) + surf := make([]byte, w*h*4) + cc.Draw(newP(surf, w), light) +} + +func TestGraphViewCornerRadiusRoundsNodesAndClusters(t *testing.T) { + const w, h = 400, 120 + draw := func(radius int) ([]byte, *GraphView) { + g := NewGraphView(manyClusters(1, 2)) + g.CornerRadius = radius + g.SetBounds(Rect{W: w, H: h}) + surf := makeSurface(w, h) + g.Draw(newP(surf, w), DefaultLight()) + g.Model.Selected.Set("node:n0_0") + g.Draw(newP(surf, w), DefaultLight()) + return surf, g + } + sq, g := draw(0) + rd, _ := draw(6) + n, _ := g.NodeBox("n0_0") + c := g.clusterBox["c0"] + if pixelAt(sq, w, n.X, n.Y) == pixelAt(rd, w, n.X, n.Y) { + t.Error("a rounded node's corner pixel equals the square one's") + } + if pixelAt(sq, w, c.X, c.Y) == pixelAt(rd, w, c.X, c.Y) { + t.Error("a rounded cluster's corner pixel equals the square one's") + } + if g.clusterRadius() != 0 { + t.Error("clusterRadius of a square graph") + } + g.CornerRadius = 8 + if g.clusterRadius() != 12 { + t.Errorf("clusterRadius %d", g.clusterRadius()) + } +} diff --git a/theme_slate.go b/theme_slate.go new file mode 100644 index 0000000..18c542c --- /dev/null +++ b/theme_slate.go @@ -0,0 +1,132 @@ +// Copyright (c) 2026 the go-widgets/toolkit authors. All rights reserved. +// Use of this source code is governed by a BSD-3-Clause license that can be +// found in the LICENSE file at the root of this repository. + +package toolkit + +// Slate is the look of a Tailwind "slate" web app with one brand accent: a +// slate-50 page, white cards with a soft border and shadow, slate-900 ink, and +// the brand colour for the accent, the primary button and the brand chip. It +// is modelled on the go-authn account page (login.mesocentre.plateau-de-saclay.net): +// `--brand:#0079a8; --brand-on:#fff; --brand-hover:#006890; --brand-ink:#0079a8` +// in light, `--brand-hover:#248cb4; --brand-ink:#4ea2c3` in dark. +// +// SlateLight and SlateDark take the brand colour as a parameter and DERIVE the +// two variants from it the way that page's values relate: hover is the brand +// darkened 14% in light and lightened 14% in dark; ink (the brand as text on a +// dark ground) is the brand lightened 31%. For #0079a8 that reproduces the page's +// #006890, #248cb4 and #4ea2c3 exactly (see the test). +// +// What a palette cannot say a Theme says in Extra, which every widget that wants +// it reads and every widget that does not ignores: +// +// OnAccent ink on an accent fill (brand-on) +// brand_hover the accent when pointed at +// brand_ink the brand as TEXT or an icon on this theme's ground +// chip_brand_bg a brand chip's fill (brand-ink at 10% over the surface) +// chip_plain_bg a plain chip's fill (slate-100 / white at 10%) +// chip_plain_ink a plain chip's text (slate-700 / slate-200) +// shadow the card shadow's colour with its alpha; absent in dark, +// where the page has none +// +// The system's light/dark preference is the HOST's to read (a browser's +// prefers-color-scheme, a desktop's setting): Slate(brand, dark) returns the +// variant to use, and the host calls it again when the preference changes. + +// The Tailwind v4 slate scale (the ramp the reference page is built from). +var ( + slate50 = RGB(0xF8, 0xFA, 0xFC) + slate100 = RGB(0xF1, 0xF5, 0xF9) + slate200 = RGB(0xE2, 0xE8, 0xF0) + slate300 = RGB(0xCB, 0xD5, 0xE1) + slate400 = RGB(0x94, 0xA3, 0xB8) + slate700 = RGB(0x33, 0x41, 0x55) + slate900 = RGB(0x0F, 0x17, 0x2A) + + // The reference page's dark ground, card and raised card. + slateDarkBg = RGB(0x12, 0x15, 0x1A) + slateDarkSurface = RGB(0x1B, 0x20, 0x27) + slateDarkRaised = RGB(0x24, 0x2B, 0x34) + + white = RGB(0xFF, 0xFF, 0xFF) +) + +// shade moves c toward black (t < 0) or white (t > 0) by |t|. +func shade(c RGBA, t float64) RGBA { + if t < 0 { + return blendRGBA(RGB(0, 0, 0), c, -t) + } + return blendRGBA(white, c, t) +} + +// SlateLight is the light Slate theme with the given brand accent. +func SlateLight(brand RGBA) *Theme { + return &Theme{ + Background: slate50, + Surface: white, + SurfaceAlt: slate100, + OnBackground: slate900, + OnSurface: slate900, + Accent: brand, + // slate-200 at 80% over white: the card border. + Border: blendRGBA(slate200, white, 0.8), + StarFilled: RGB(0xF5, 0xB8, 0x00), + StarEmpty: slate300, + Extra: map[string]RGBA{ + "OnAccent": white, + "brand_hover": shade(brand, -0.14), + "brand_ink": brand, + "chip_brand_bg": blendRGBA(brand, white, 0.10), + "chip_plain_bg": slate100, + "chip_plain_ink": slate700, + "muted": slate400, + // #101828 at 4% and 8%: the .surface shadow's two layers. + "shadow": RGBA{R: 0x10, G: 0x18, B: 0x28, A: 0x14}, + }, + } +} + +// SlateDark is the dark Slate theme with the given brand accent. The accent +// itself stays the brand (a fill with white ink); text and icons in the brand +// use brand_ink, which is lightened to read on the dark ground. +func SlateDark(brand RGBA) *Theme { + return &Theme{ + Background: slateDarkBg, + Surface: slateDarkSurface, + SurfaceAlt: slateDarkRaised, + OnBackground: slate100, + OnSurface: slate100, + Accent: brand, + // white at 7% over the card: the card border. + Border: blendRGBA(white, slateDarkSurface, 0.07), + StarFilled: RGB(0xFF, 0xC9, 0x33), + StarEmpty: slate700, + Extra: map[string]RGBA{ + "OnAccent": white, + "brand_hover": shade(brand, 0.14), + "brand_ink": shade(brand, 0.306), + "chip_brand_bg": blendRGBA(shade(brand, 0.306), slateDarkSurface, 0.10), + "chip_plain_bg": blendRGBA(white, slateDarkSurface, 0.10), + "chip_plain_ink": slate200, + "muted": slate400, + }, + } +} + +// Slate returns SlateDark(brand) when dark, else SlateLight(brand): the theme a +// host picks from the system's colour-scheme preference. +func Slate(brand RGBA, dark bool) *Theme { + if dark { + return SlateDark(brand) + } + return SlateLight(brand) +} + +// themeInk is the brand-as-text colour of a theme: Extra["brand_ink"] when the +// theme carries one, else its Accent. +func themeInk(theme *Theme) RGBA { + if c, ok := theme.Extra["brand_ink"]; ok { + return c + } + return theme.Accent +} diff --git a/theme_slate_test.go b/theme_slate_test.go new file mode 100644 index 0000000..31828fb --- /dev/null +++ b/theme_slate_test.go @@ -0,0 +1,103 @@ +// Copyright (c) 2026 the go-widgets/toolkit authors. All rights reserved. +// Use of this source code is governed by a BSD-3-Clause license that can be +// found in the LICENSE file at the root of this repository. + +package toolkit + +import "testing" + +var slateBrand = RGB(0x00, 0x79, 0xA8) // the go-authn account page's brand + +// near reports whether two colours agree within 1 per channel. +func near(a, b RGBA) bool { + d := func(x, y uint8) bool { + if x > y { + return x-y <= 1 + } + return y-x <= 1 + } + return d(a.R, b.R) && d(a.G, b.G) && d(a.B, b.B) +} + +// The derived brand variants reproduce the reference page's own values. +func TestSlateDerivesTheReferencePagesBrandVariants(t *testing.T) { + l, d := SlateLight(slateBrand), SlateDark(slateBrand) + for name, c := range map[string][2]RGBA{ + "light hover (#006890)": {l.Extra["brand_hover"], RGB(0x00, 0x68, 0x90)}, + "light ink (#0079a8)": {l.Extra["brand_ink"], RGB(0x00, 0x79, 0xA8)}, + "dark hover (#248cb4)": {d.Extra["brand_hover"], RGB(0x24, 0x8C, 0xB4)}, + "dark ink (#4ea2c3)": {d.Extra["brand_ink"], RGB(0x4E, 0xA2, 0xC3)}, + } { + if !near(c[0], c[1]) { + t.Errorf("%s: got %+v, want %+v", name, c[0], c[1]) + } + } +} + +func TestSlatePalettes(t *testing.T) { + l, d := SlateLight(slateBrand), SlateDark(slateBrand) + assertPalette(t, "SlateLight", l, + op(0xF8, 0xFA, 0xFC), // Background: slate-50 + op(0xFF, 0xFF, 0xFF), // Surface: white card + op(0xF1, 0xF5, 0xF9), // SurfaceAlt: slate-100 + op(0x0F, 0x17, 0x2A), // OnBackground: slate-900 + op(0x0F, 0x17, 0x2A), // OnSurface + op(0x00, 0x79, 0xA8), // Accent: the brand + l.Border, // Border: derived, pinned below + op(0xFF, 0xFF, 0xFF), // OnAccent + ) + assertPalette(t, "SlateDark", d, + op(0x12, 0x15, 0x1A), // dark-bg + op(0x1B, 0x20, 0x27), // dark-surface + op(0x24, 0x2B, 0x34), // dark-raised + op(0xF1, 0xF5, 0xF9), // slate-100 + op(0xF1, 0xF5, 0xF9), + op(0x00, 0x79, 0xA8), + d.Border, + op(0xFF, 0xFF, 0xFF), + ) + // The card border is a soft tone of its ground, not an ink. + if !near(l.Border, RGB(0xE8, 0xED, 0xF3)) { + t.Errorf("light border %+v", l.Border) + } + if !near(d.Border, RGB(0x2A, 0x2F, 0x36)) { + t.Errorf("dark border %+v", d.Border) + } + // Light has the card shadow, dark does not (the page has none). + if l.Extra["shadow"].A == 0 { + t.Error("light theme has no shadow") + } + if _, ok := d.Extra["shadow"]; ok { + t.Error("dark theme has a shadow") + } + for _, th := range []*Theme{l, d} { + for _, k := range []string{"OnAccent", "brand_hover", "brand_ink", "chip_brand_bg", "chip_plain_bg", "chip_plain_ink", "muted"} { + if _, ok := th.Extra[k]; !ok { + t.Errorf("Extra[%q] missing", k) + } + } + } +} + +func TestSlateFollowsThePreference(t *testing.T) { + if Slate(slateBrand, false).Background != slate50 || Slate(slateBrand, true).Background != slateDarkBg { + t.Error("Slate(brand, dark) must pick the variant") + } + // Any brand works: the accent is the parameter. + other := RGB(0x7C, 0x3A, 0xED) + if SlateLight(other).Accent != other || SlateDark(other).Accent != other { + t.Error("the accent is not the brand parameter") + } +} + +func TestShadeAndThemeInk(t *testing.T) { + if shade(RGB(100, 100, 100), -1) != RGB(0, 0, 0) || shade(RGB(100, 100, 100), 1) != RGB(255, 255, 255) { + t.Error("shade extremes") + } + if themeInk(SlateDark(slateBrand)) != SlateDark(slateBrand).Extra["brand_ink"] { + t.Error("themeInk must prefer brand_ink") + } + if themeInk(DefaultLight()) != DefaultLight().Accent { + t.Error("themeInk falls back to the accent") + } +}