oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1package theme
2
3import (
4 "fmt"
5 "math"
6 "strings"
7)
8
9// App is one product of the suite and its place on Calma's colour ring.
10type App struct {
11 Slug string // closed set; see Apps
12 Name string // the product's name, as a person reads it
13 Hue float64 // OKLCH hue, degrees
14}
15
16// apps is the ring. Every app's colour is the same OKLCH lightness and
17// chroma at a different hue, so no app is louder than another; the hues
18// are spaced evenly to the eye rather than to the protractor (34-50°,
19// with the widest gap across the yellow-greens, which go muddy at this
20// lightness). Paul, 2026-09-29, with Mail and Files still to come.
21// Mapa joined at 125 between Fichas and Sheets (Paul, 2026-11-13: "more
22// related to Fichas than it is to Memoria or Mail"). Equipo joined at 322
23// between Memoria and Mail (Paul, 2026-11-13: "Co-ordinator in Pink").
24// Contra joined at 200 between Files and Calls (Paul, 2026-11-21: "Colour
25// seems fine").
26//
27// Oficina and Docs share 262 on purpose: the suite's own accent, used
28// where no app is in charge (sign-in, the team's home), is the ring's blue,
29// and Docs is where that blue started. Oficina's mark is every hue, not
30// this one.
31//
32// This table is the one source of the suite's colours: the accent blocks
33// below, and brand/icons.json's hues (brand_test checks they agree).
34var apps = []App{
35 {"oficina", "Oficina", 262},
36 {"docs", "Docs", 262},
37 {"memoria", "Memoria", 302},
38 {"equipo", "Equipo", 322},
39 {"mail", "Mail", 342},
40 {"calendar", "Calendar", 20},
41 {"slides", "Slides", 56},
42 {"fichas", "Fichas", 90},
43 {"mapa", "Mapa", 125},
44 {"sheets", "Sheets", 140},
45 {"files", "Files", 180},
46 {"contra", "Contra", 200},
47 {"calls", "Calls", 220},
48}
49
50// Apps returns the ring, Oficina first.
51func Apps() []App { return append([]App(nil), apps...) }
52
53// Lookup returns the app for slug. The set is closed: a slug that is not
54// in the ring is not a theme, whatever else it is.
55func Lookup(slug string) (App, bool) {
56 for _, a := range apps {
57 if a.Slug == slug {
58 return a, true
59 }
60 }
61 return App{}, false
62}
63
64// The accent's four tokens, as OKLCH lightness and chroma; the hue is the
65// app's. Light: a deep accent that carries white text; dark: a light one
66// that carries near-black. Chroma is the most each hue can hold at that
67// lightness inside sRGB, up to these values (see Hex).
68var accentSpec = [2]struct{ accent, strong, soft [2]float64 }{
69 {accent: [2]float64{0.49, 0.15}, strong: [2]float64{0.43, 0.14}, soft: [2]float64{0.955, 0.025}},
70 {accent: [2]float64{0.76, 0.12}, strong: [2]float64{0.84, 0.09}, soft: [2]float64{0.29, 0.045}},
71}
72
73// AccentCSS is the block that re-declares the accent for one app. It
74// follows estilo.css in the cascade, so the app's colour wins everywhere
75// the suite's would have shown.
76func AccentCSS(slug string) ([]byte, bool) {
77 a, ok := Lookup(slug)
78 if !ok {
79 return nil, false
80 }
81 hex := func(lc [2]float64) string { return Hex(lc[0], lc[1], a.Hue) }
82 l, d := accentSpec[0], accentSpec[1]
83 var b strings.Builder
84 fmt.Fprintf(&b, "/* The accent for %s: hue %g on Calma's ring (estilo theme/apps.go). */\n", a.Name, a.Hue)
85 b.WriteString(":root {\n")
86 fmt.Fprintf(&b, " --rst-accent: light-dark(%s, %s);\n", hex(l.accent), hex(d.accent))
87 fmt.Fprintf(&b, " --rst-accent-strong: light-dark(%s, %s);\n", hex(l.strong), hex(d.strong))
88 fmt.Fprintf(&b, " --rst-accent-soft: light-dark(%s, %s);\n", hex(l.soft), hex(d.soft))
89 b.WriteString(" --rst-on-accent: light-dark(#ffffff, #111418);\n")
90 b.WriteString("}\n")
91 return []byte(b.String()), true
92}
93
94// ForApp is the whole theme for one app: estilo.css with that app's
95// accent after it. Serve it as the app's /static/theme.css.
96func ForApp(slug string) ([]byte, bool) {
97 accent, ok := AccentCSS(slug)
98 if !ok {
99 return nil, false
100 }
101 out := append(CSS(), '\n')
102 return append(out, accent...), true
103}
104
105// Hex converts an OKLCH colour to sRGB hex, taking chroma down in small
106// steps until the colour fits in sRGB, so the hue and lightness stay true
107// and only the saturation gives. (Clipping each channel instead would
108// shift the hue, which is the one thing the ring promises.)
109func Hex(L, C, h float64) string {
110 for C > 0 {
111 if r, g, b := oklchToLinear(L, C, h); inGamut(r) && inGamut(g) && inGamut(b) {
112 break
113 }
114 C -= 0.002
115 }
116 r, g, b := oklchToLinear(L, math.Max(C, 0), h)
117 return fmt.Sprintf("#%02x%02x%02x", encode(r), encode(g), encode(b))
118}
119
120func inGamut(c float64) bool { return c >= -1e-4 && c <= 1+1e-4 }
121
122// oklchToLinear is Björn Ottosson's OKLab to linear sRGB.
123func oklchToLinear(L, C, h float64) (r, g, b float64) {
124 rad := h * math.Pi / 180
125 a, bb := C*math.Cos(rad), C*math.Sin(rad)
126 l := cube(L + 0.3963377774*a + 0.2158037573*bb)
127 m := cube(L - 0.1055613458*a - 0.0638541728*bb)
128 s := cube(L - 0.0894841775*a - 1.2914855480*bb)
129 return 4.0767416621*l - 3.3077115913*m + 0.2309699292*s,
130 -1.2684380046*l + 2.6097574011*m - 0.3413193965*s,
131 -0.0041960863*l - 0.7034186147*m + 1.7076147010*s
132}
133
134func cube(x float64) float64 { return x * x * x }
135
136func encode(c float64) int {
137 c = math.Max(0, math.Min(1, c))
138 if c <= 0.0031308 {
139 c *= 12.92
140 } else {
141 c = 1.055*math.Pow(c, 1/2.4) - 0.055
142 }
143 return int(math.Round(c * 255))
144}
145