oklab

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2022 License: MIT Imports: 2 Imported by: 0

README

An Oklab color space for Go.
Oklch is the same color space with polar coordinates.

See https://bottosson.github.io/posts/oklab/

Documentation

Overview

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

Example (ConvertOklabToRGB)
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
Example (ConvertToOklab)
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
Example (GradientImage)
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>

Index

Examples

Constants

This section is empty.

Variables

View Source
var OklabModel = color.ModelFunc(oklabModel)
View Source
var OklchModel = color.ModelFunc(oklchModel)

Functions

This section is empty.

Types

type Oklab

type Oklab struct {
	L float64 // Perceived lightness
	A float64 // How green/red the color is
	B float64 // How blue/yellow the color is
}

func (Oklab) LinearSRGB

func (c Oklab) LinearSRGB() (float64, float64, float64)

Convert to linear sRGB. See https://bottosson.github.io/posts/oklab/

func (Oklab) Oklch

func (c Oklab) Oklch() Oklch

Convert to LCh, which is Oklab in polar.

func (Oklab) RGBA

func (c Oklab) RGBA() (uint32, uint32, uint32, uint32)

See image.Color.

type Oklch

type Oklch struct {
	L float64 // Perceived lightness
	C float64 // Chroma
	H float64 // Hue
}

func (Oklch) Oklab

func (c Oklch) Oklab() Oklab

Convert to Oklab.

func (Oklch) RGBA

func (c Oklch) RGBA() (uint32, uint32, uint32, uint32)

See image.Color.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL