github.com/Seikaijyu/gio@v0.0.1/internal/f32color/rgba_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package f32color
     4  
     5  import (
     6  	"image/color"
     7  	"testing"
     8  )
     9  
    10  func TestNRGBAToLinearRGBA_Boundary(t *testing.T) {
    11  	for col := 0; col <= 0xFF; col++ {
    12  		for alpha := 0; alpha <= 0xFF; alpha++ {
    13  			in := color.NRGBA{R: uint8(col), A: uint8(alpha)}
    14  			premul := NRGBAToLinearRGBA(in)
    15  			if premul.A != uint8(alpha) {
    16  				t.Errorf("%v: got %v expected %v", in, premul.A, alpha)
    17  			}
    18  			if premul.R > premul.A {
    19  				t.Errorf("%v: R=%v > A=%v", in, premul.R, premul.A)
    20  			}
    21  		}
    22  	}
    23  }
    24  
    25  func TestLinearToRGBARoundtrip(t *testing.T) {
    26  	for col := 0; col <= 0xFF; col++ {
    27  		for alpha := 0; alpha <= 0xFF; alpha++ {
    28  			want := color.NRGBA{R: uint8(col), A: uint8(alpha)}
    29  			if alpha == 0 {
    30  				want.R = 0
    31  			}
    32  			got := LinearFromSRGB(want).SRGB()
    33  			if want != got {
    34  				t.Errorf("got %v expected %v", got, want)
    35  			}
    36  		}
    37  	}
    38  }
    39  
    40  var sink RGBA
    41  
    42  func BenchmarkLinearFromSRGB(b *testing.B) {
    43  	b.Run("opaque", func(b *testing.B) {
    44  		for i := 0; i < b.N; i++ {
    45  			sink = LinearFromSRGB(color.NRGBA{R: byte(i), G: byte(i >> 8), B: byte(i >> 16), A: 0xFF})
    46  		}
    47  	})
    48  	b.Run("translucent", func(b *testing.B) {
    49  		for i := 0; i < b.N; i++ {
    50  			sink = LinearFromSRGB(color.NRGBA{R: byte(i), G: byte(i >> 8), B: byte(i >> 16), A: 0x50})
    51  		}
    52  	})
    53  	b.Run("transparent", func(b *testing.B) {
    54  		for i := 0; i < b.N; i++ {
    55  			sink = LinearFromSRGB(color.NRGBA{R: byte(i), G: byte(i >> 8), B: byte(i >> 16), A: 0x00})
    56  		}
    57  	})
    58  }