oklab package - github.com/alltom/oklab - Go Packages

Package oklab implements the Oklab color space, as described at https://bottosson.github.io/posts/oklab/

package main

import (
	"fmt"
	"github.com/alltom/oklab"
)

func main() {
	oklabc := oklab.Oklab{L: 0.9322421414586456, A: 0.03673270292094283, B: 0.0006123556644819055}
	r, g, b, _ := oklabc.RGBA()
	fmt.Printf("R: 0x%x, G: 0x%x, B: 0x%x\n", r>>8, g>>8, b>>8)
}
Output:

R: 0xff, G: 0xdf, B: 0xe7
package main

import (
	"fmt"
	"github.com/alltom/oklab"
	"image/color"
)

func main() {
	rgbc := color.RGBA{0xff, 0xdf, 0xe7, 0xff}
	oklabc := oklab.OklabModel.Convert(rgbc).(oklab.Oklab)
	fmt.Printf("L: %.2f, a: %.2f, b: %.2f\n", oklabc.L, oklabc.A, oklabc.B)
}
Output:

L: 0.93, a: 0.04, b: 0.00
package main

import (
	"fmt"
	"github.com/alltom/oklab"
	"image"
	"image/color"
	"image/png"
	"os"
)

func main() {
	f, _ := os.Create("gradient.png")
	png.Encode(f, GradientImage{})
	fmt.Println("err =", f.Close())
}

type GradientImage struct{}

func (g GradientImage) ColorModel() color.Model {
	return oklab.OklabModel
}

func (g GradientImage) Bounds() image.Rectangle {
	return image.Rect(0, 0, 1200, 600)
}

func (g GradientImage) At(x, y int) color.Color {
	a := lerp(float64(x)/float64(g.Bounds().Dx()), -0.233888, 0.276216)
	b := lerp(float64(y)/float64(g.Bounds().Dy()), -0.311528, 0.198570)
	return oklab.Oklab{0.8, a, b}
}

func lerp(x, min, max float64) float64 {
	return x*(max-min) + min
}
Output:

err = <nil>

This section is empty.

This section is empty.

func (c Oklab) Oklch() Oklch

Convert to LCh, which is Oklab in polar.

func (c Oklch) Oklab() Oklab

Convert to Oklab.