feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
+182
@@ -0,0 +1,182 @@
|
||||
// Package gamut converts between sRGB, Display P3, Oklab and Oklch, and maps
|
||||
// colors into a display's gamut as CSS Color 4 does.
|
||||
//
|
||||
// Colors travel as extended sRGB: gamma encoded with the sRGB curve,
|
||||
// mirrored around zero, so that a color outside the sRGB gamut (but inside
|
||||
// Display P3's) has components below 0 or above 1.
|
||||
package gamut
|
||||
|
||||
import "math"
|
||||
|
||||
// Space is an RGB gamut colors are mapped into.
|
||||
type Space int
|
||||
|
||||
const (
|
||||
SRGB Space = iota
|
||||
DisplayP3
|
||||
)
|
||||
|
||||
// Decode returns the linear light of an sRGB-encoded component, which may be
|
||||
// outside 0 to 1.
|
||||
func Decode(v float64) float64 {
|
||||
a := math.Abs(v)
|
||||
if a <= 0.04045 {
|
||||
return v / 12.92
|
||||
}
|
||||
return math.Copysign(math.Pow((a+0.055)/1.055, 2.4), v)
|
||||
}
|
||||
|
||||
// Encode is the inverse of Decode.
|
||||
func Encode(v float64) float64 {
|
||||
a := math.Abs(v)
|
||||
if a <= 0.0031308 {
|
||||
return v * 12.92
|
||||
}
|
||||
return math.Copysign(1.055*math.Pow(a, 1/2.4)-0.055, v)
|
||||
}
|
||||
|
||||
// Oklab converts linear sRGB to Oklab.
|
||||
func Oklab(r, g, b float64) (l, a, bb float64) {
|
||||
lc := math.Cbrt(0.4122214708*r + 0.5363325363*g + 0.0514459929*b)
|
||||
mc := math.Cbrt(0.2119034982*r + 0.6806995451*g + 0.1073969566*b)
|
||||
sc := math.Cbrt(0.0883024619*r + 0.2817188376*g + 0.6299787005*b)
|
||||
return 0.2104542553*lc + 0.7936177850*mc - 0.0040720468*sc,
|
||||
1.9779984951*lc - 2.4285922050*mc + 0.4505937099*sc,
|
||||
0.0259040371*lc + 0.7827717662*mc - 0.8086757660*sc
|
||||
}
|
||||
|
||||
// LinearSRGB converts Oklab to linear sRGB, unclamped.
|
||||
func LinearSRGB(l, a, b float64) (r, g, bb float64) {
|
||||
lc := l + 0.3963377774*a + 0.2158037573*b
|
||||
mc := l - 0.1055613458*a - 0.0638541728*b
|
||||
sc := l - 0.0894841775*a - 1.2914855480*b
|
||||
lc, mc, sc = lc*lc*lc, mc*mc*mc, sc*sc*sc
|
||||
return 4.0767416621*lc - 3.3077115913*mc + 0.2309699292*sc,
|
||||
-1.2684380046*lc + 2.6097574011*mc - 0.3413193965*sc,
|
||||
-0.0041960863*lc - 0.7034186147*mc + 1.7076147010*sc
|
||||
}
|
||||
|
||||
// LinearSRGBToP3 converts linear sRGB to linear Display P3.
|
||||
func LinearSRGBToP3(r, g, b float64) (float64, float64, float64) {
|
||||
return 0.8224621*r + 0.1775380*g,
|
||||
0.0331941*r + 0.9668058*g,
|
||||
0.0170827*r + 0.0723974*g + 0.9105199*b
|
||||
}
|
||||
|
||||
// LinearP3ToSRGB converts linear Display P3 to linear sRGB.
|
||||
func LinearP3ToSRGB(r, g, b float64) (float64, float64, float64) {
|
||||
return 1.2249401*r - 0.2249404*g,
|
||||
-0.0420569*r + 1.0420571*g,
|
||||
-0.0196376*r - 0.0786361*g + 1.0982735*b
|
||||
}
|
||||
|
||||
// Oklch converts Oklab to Oklch, with the hue in degrees.
|
||||
func Oklch(l, a, b float64) (float64, float64, float64) {
|
||||
h := math.Atan2(b, a) * 180 / math.Pi
|
||||
if h < 0 {
|
||||
h += 360
|
||||
}
|
||||
return l, math.Hypot(a, b), h
|
||||
}
|
||||
|
||||
// FromOklch converts Oklch, with the hue in degrees, to Oklab.
|
||||
func FromOklch(l, c, h float64) (float64, float64, float64) {
|
||||
s, co := math.Sincos(h * math.Pi / 180)
|
||||
return l, c * co, c * s
|
||||
}
|
||||
|
||||
// toSpace converts an Oklab color to linear RGB of the space.
|
||||
func toSpace(sp Space, l, a, b float64) (float64, float64, float64) {
|
||||
r, g, bb := LinearSRGB(l, a, b)
|
||||
if sp == DisplayP3 {
|
||||
return LinearSRGBToP3(r, g, bb)
|
||||
}
|
||||
return r, g, bb
|
||||
}
|
||||
|
||||
// fromSpace converts linear RGB of the space to Oklab.
|
||||
func fromSpace(sp Space, r, g, b float64) (float64, float64, float64) {
|
||||
if sp == DisplayP3 {
|
||||
r, g, b = LinearP3ToSRGB(r, g, b)
|
||||
}
|
||||
return Oklab(r, g, b)
|
||||
}
|
||||
|
||||
const (
|
||||
// jnd is the Oklab distance CSS Color 4 treats as imperceptible.
|
||||
jnd = 0.02
|
||||
// epsilon ends the search for the chroma, and absorbs the rounding of
|
||||
// the matrices when testing for the gamut.
|
||||
epsilon = 0.0001
|
||||
inGamut = 0.00001
|
||||
)
|
||||
|
||||
func within(r, g, b float64) bool {
|
||||
in := func(v float64) bool { return v >= -inGamut && v <= 1+inGamut }
|
||||
return in(r) && in(g) && in(b)
|
||||
}
|
||||
|
||||
func clip(v float64) float64 { return max(0, min(v, 1)) }
|
||||
|
||||
// maxChroma is beyond the chroma of any color a display shows: Map starts
|
||||
// its search there for a larger one, as an infinite one.
|
||||
const maxChroma = 1
|
||||
|
||||
// Map returns the Oklch color as linear RGB inside the gamut of sp. It
|
||||
// reduces the chroma, keeping lightness and hue, until clipping what is left
|
||||
// out changes the color imperceptibly (the algorithm of CSS Color 4,
|
||||
// "gamut mapping to a destination color space"). A negative or NaN chroma
|
||||
// is 0, as CSS clamps it, a NaN lightness 0 and a hue that is not finite 0.
|
||||
func Map(sp Space, l, c, h float64) (r, g, b float64) {
|
||||
if l >= 1 {
|
||||
return 1, 1, 1
|
||||
}
|
||||
if !(l > 0) {
|
||||
return 0, 0, 0
|
||||
}
|
||||
if !(c > 0) {
|
||||
c = 0
|
||||
}
|
||||
c = min(c, maxChroma)
|
||||
if math.IsNaN(h) || math.IsInf(h, 0) {
|
||||
h = 0
|
||||
}
|
||||
_, oa, ob := FromOklch(l, c, h)
|
||||
r, g, b = toSpace(sp, l, oa, ob)
|
||||
if within(r, g, b) {
|
||||
return clip(r), clip(g), clip(b)
|
||||
}
|
||||
// distance is the Oklab distance between the clipped color and lab.
|
||||
clipped := func(a, bb float64) (cr, cg, cb, e float64) {
|
||||
r, g, b := toSpace(sp, l, a, bb)
|
||||
cr, cg, cb = clip(r), clip(g), clip(b)
|
||||
cl, ca, cbb := fromSpace(sp, cr, cg, cb)
|
||||
return cr, cg, cb, math.Sqrt((cl-l)*(cl-l) + (ca-a)*(ca-a) + (cbb-bb)*(cbb-bb))
|
||||
}
|
||||
cr, cg, cb, e := clipped(oa, ob)
|
||||
if e < jnd {
|
||||
return cr, cg, cb
|
||||
}
|
||||
lo, hi := 0.0, c
|
||||
loIn := true
|
||||
for hi-lo > epsilon {
|
||||
mid := (lo + hi) / 2
|
||||
_, a, bb := FromOklch(l, mid, h)
|
||||
r, g, b := toSpace(sp, l, a, bb)
|
||||
if loIn && within(r, g, b) {
|
||||
lo = mid
|
||||
continue
|
||||
}
|
||||
cr, cg, cb, e = clipped(a, bb)
|
||||
if e < jnd {
|
||||
if jnd-e < epsilon {
|
||||
return cr, cg, cb
|
||||
}
|
||||
loIn = false
|
||||
lo = mid
|
||||
} else {
|
||||
hi = mid
|
||||
}
|
||||
}
|
||||
return cr, cg, cb
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package gamut
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func near(a, b, tol float64) bool { return math.Abs(a-b) <= tol }
|
||||
|
||||
func TestOklabKnownColors(t *testing.T) {
|
||||
// Reference values from Björn Ottosson's Oklab post.
|
||||
l, a, b := Oklab(1, 1, 1)
|
||||
if !near(l, 1, 1e-4) || !near(a, 0, 1e-4) || !near(b, 0, 1e-4) {
|
||||
t.Errorf("white = %v %v %v", l, a, b)
|
||||
}
|
||||
l, a, b = Oklab(1, 0, 0)
|
||||
if !near(l, 0.6280, 1e-3) || !near(a, 0.2249, 1e-3) || !near(b, 0.1258, 1e-3) {
|
||||
t.Errorf("red = %v %v %v", l, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOklabRoundTrip(t *testing.T) {
|
||||
for _, c := range [][3]float64{{0.2, 0.5, 0.8}, {0, 0, 0}, {1, 0.1, 0.9}, {-0.1, 0.4, 1.1}} {
|
||||
l, a, b := Oklab(c[0], c[1], c[2])
|
||||
r, g, bb := LinearSRGB(l, a, b)
|
||||
if !near(r, c[0], 1e-5) || !near(g, c[1], 1e-5) || !near(bb, c[2], 1e-5) {
|
||||
t.Errorf("%v came back as %v %v %v", c, r, g, bb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeDecode(t *testing.T) {
|
||||
for _, v := range []float64{-0.5, -0.01, 0, 0.001, 0.5, 1, 1.3} {
|
||||
if got := Decode(Encode(v)); !near(got, v, 1e-9) {
|
||||
t.Errorf("Decode(Encode(%v)) = %v", v, got)
|
||||
}
|
||||
}
|
||||
if !near(Encode(0.5), 0.7354, 1e-3) {
|
||||
t.Errorf("Encode(0.5) = %v", Encode(0.5))
|
||||
}
|
||||
}
|
||||
|
||||
func TestP3Matrices(t *testing.T) {
|
||||
r, g, b := LinearP3ToSRGB(LinearSRGBToP3(0.3, 0.6, 0.9))
|
||||
if !near(r, 0.3, 1e-4) || !near(g, 0.6, 1e-4) || !near(b, 0.9, 1e-4) {
|
||||
t.Errorf("round trip = %v %v %v", r, g, b)
|
||||
}
|
||||
// P3 white is sRGB white.
|
||||
r, g, b = LinearP3ToSRGB(1, 1, 1)
|
||||
if !near(r, 1, 1e-4) || !near(g, 1, 1e-4) || !near(b, 1, 1e-4) {
|
||||
t.Errorf("white = %v %v %v", r, g, b)
|
||||
}
|
||||
// Pure P3 green lies outside sRGB.
|
||||
if _, g, _ := LinearP3ToSRGB(0, 1, 0); g <= 1 {
|
||||
t.Errorf("P3 green in sRGB has g = %v, want above 1", g)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapInGamutIsExact(t *testing.T) {
|
||||
l, a, b := Oklab(Decode(0.2), Decode(0.5), Decode(0.8))
|
||||
l, c, h := Oklch(l, a, b)
|
||||
r, g, bb := Map(SRGB, l, c, h)
|
||||
if !near(Encode(r), 0.2, 1e-3) || !near(Encode(g), 0.5, 1e-3) || !near(Encode(bb), 0.8, 1e-3) {
|
||||
t.Errorf("mapped to %v %v %v", Encode(r), Encode(g), Encode(bb))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapKeepsLightnessAndHue(t *testing.T) {
|
||||
for _, sp := range []Space{SRGB, DisplayP3} {
|
||||
for _, c := range [][3]float64{{0.7, 0.4, 150}, {0.5, 0.35, 30}, {0.9, 0.3, 200}, {0.3, 0.3, 280}} {
|
||||
r, g, b := Map(sp, c[0], c[1], c[2])
|
||||
if !within(r, g, b) {
|
||||
t.Errorf("space %d, %v: %v %v %v outside the gamut", sp, c, r, g, b)
|
||||
}
|
||||
if sp == DisplayP3 {
|
||||
r, g, b = LinearP3ToSRGB(r, g, b)
|
||||
}
|
||||
l, a, bb := Oklab(r, g, b)
|
||||
l, ch, h := Oklch(l, a, bb)
|
||||
if !near(l, c[0], 0.03) {
|
||||
t.Errorf("space %d, %v: lightness %v", sp, c, l)
|
||||
}
|
||||
if ch > 0.02 && math.Abs(math.Mod(h-c[2]+540, 360)-180) > 10 {
|
||||
t.Errorf("space %d, %v: hue %v", sp, c, h)
|
||||
}
|
||||
if ch > c[1]+1e-6 {
|
||||
t.Errorf("space %d, %v: chroma grew to %v", sp, c, ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapP3KeepsMoreThanSRGB(t *testing.T) {
|
||||
// A vivid green is further from gray in Display P3 than in sRGB.
|
||||
chroma := func(sp Space) float64 {
|
||||
r, g, b := Map(sp, 0.85, 0.3, 145)
|
||||
l, a, bb := fromSpace(sp, r, g, b)
|
||||
_, c, _ := Oklch(l, a, bb)
|
||||
return c
|
||||
}
|
||||
if cs, cp := chroma(SRGB), chroma(DisplayP3); cp <= cs {
|
||||
t.Errorf("chroma: sRGB %v, P3 %v", cs, cp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapOddInput(t *testing.T) {
|
||||
gray := func(sp Space, l, c, h float64) {
|
||||
t.Helper()
|
||||
r, g, b := Map(sp, l, c, h)
|
||||
if !near(r, g, 1e-6) || !near(g, b, 1e-6) {
|
||||
t.Errorf("Map(%v, %v, %v) = %v %v %v, want a gray", l, c, h, r, g, b)
|
||||
}
|
||||
}
|
||||
// A negative chroma is 0, as CSS clamps it, not the opposite hue.
|
||||
gray(SRGB, 0.7, -0.1, 30)
|
||||
gray(DisplayP3, 0.7, math.NaN(), 30)
|
||||
// An infinite chroma ends, as the most vivid color of the hue.
|
||||
inf := math.Inf(1)
|
||||
r, g, b := Map(SRGB, 0.5, inf, 30)
|
||||
r2, g2, b2 := Map(SRGB, 0.5, 0.6, 30)
|
||||
if !near(r, r2, 1e-3) || !near(g, g2, 1e-3) || !near(b, b2, 1e-3) {
|
||||
t.Errorf("infinite chroma = %v %v %v, want %v %v %v", r, g, b, r2, g2, b2)
|
||||
}
|
||||
for _, c := range [][3]float64{{math.NaN(), 0.2, 30}, {0.5, 0.2, inf}, {0.5, 0.2, math.NaN()}, {0.5, inf, inf}} {
|
||||
r, g, b := Map(DisplayP3, c[0], c[1], c[2])
|
||||
if !within(r, g, b) || math.IsNaN(r+g+b) {
|
||||
t.Errorf("Map(%v) = %v %v %v", c, r, g, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapExtremes(t *testing.T) {
|
||||
if r, g, b := Map(SRGB, 1.2, 0.3, 40); r != 1 || g != 1 || b != 1 {
|
||||
t.Errorf("light = %v %v %v", r, g, b)
|
||||
}
|
||||
if r, g, b := Map(DisplayP3, -0.1, 0.3, 40); r != 0 || g != 0 || b != 0 {
|
||||
t.Errorf("dark = %v %v %v", r, g, b)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user