Source file test/fixedbugs/issue81000.go

     1  // run
     2  
     3  // Copyright 2026 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  // Derived and simplified from (TestSkimageLabToRGBOutOfGamut):
    10  //   https://github.com/go-gfx/gfx/blob/main/color/skimage_test.go#L113C6-L113C35
    11  
    12  import (
    13  	"fmt"
    14  	"math"
    15  )
    16  
    17  type Lab struct {
    18  	L, A, B float64
    19  }
    20  
    21  func QuantizeUnitToByte(v float64) uint8 {
    22  	if v <= 0 {
    23  		return 0
    24  	}
    25  
    26  	if v >= 1 {
    27  		return 255
    28  	}
    29  
    30  	return uint8(math.RoundToEven(v * 255))
    31  }
    32  
    33  func main() {
    34  	labs := []Lab{
    35  		{0.7222675830801158, -0.9031940815103348, 1.301887514238097},
    36  		{0.5782818712019121, 0.4553904020428021, -0.6231537217892238},
    37  		{1.6473994133908043, 0.699861704410822, 0.3917817900198793},
    38  		{-4.466488923617846, 0.3197751169737742, 0.8014614071926937},
    39  		{0.33561687336240814, 1.0179747852290575, -0.23495942874439518},
    40  	}
    41  	want := [][3]uint8{{184, 0, 255}, {147, 116, 0}, {255, 178, 100}, {0, 82, 204}, {86, 255, 0}}
    42  	for i, l := range labs {
    43  		r, g, b := l.L, l.A, l.B
    44  		got := [3]uint8{QuantizeUnitToByte(r), QuantizeUnitToByte(g), QuantizeUnitToByte(b)}
    45  		if got != want[i] {
    46  			panic(fmt.Sprintf("issue81000: %+v = %v, want %v\n", l, got, want[i]))
    47  		}
    48  	}
    49  }
    50  

View as plain text