...

Source file src/gonum.org/v1/plot/palette/hsva_test.go

Documentation: gonum.org/v1/plot/palette

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Copyright ©2011-2012 The bíogo Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package palette
    10  
    11  import (
    12  	"image/color"
    13  	"testing"
    14  )
    15  
    16  func withinEpsilon(a, b, epsilon uint32) bool {
    17  	d := int64(a) - int64(b)
    18  	if d < 0 {
    19  		d = -d
    20  	}
    21  	return d <= int64(epsilon)
    22  }
    23  
    24  func TestColor(t *testing.T) {
    25  	for r := 0; r < 256; r += 5 {
    26  		for g := 0; g < 256; g += 5 {
    27  			for b := 0; b < 256; b += 5 {
    28  				col := color.RGBA{uint8(r), uint8(g), uint8(b), 0}
    29  				cDirectR, cDirectG, cDirectB, cDirectA := col.RGBA()
    30  				hsva := rgbaToHsva(col.RGBA())
    31  				if hsva.H < 0 || hsva.H > 1 {
    32  					t.Errorf("unexpected values for H: want [0, 1] got:%f", hsva.H)
    33  				}
    34  				if hsva.S < 0 || hsva.S > 1 {
    35  					t.Errorf("unexpected values for S: want [0, 1] got:%f", hsva.S)
    36  				}
    37  				if hsva.V < 0 || hsva.V > 1 {
    38  					t.Errorf("unexpected values for V: want [0, 1] got:%f", hsva.V)
    39  				}
    40  
    41  				var cFromHSVR, cFromHSVG, cFromHSVB, cFromHSVA uint32 = hsva.RGBA()
    42  				if cFromHSVR > 0xFFFF {
    43  					t.Errorf("unexpected values for H: want [0x0, 0xFFFF] got:%f", hsva.H)
    44  				}
    45  				if cFromHSVG > 0xFFFF {
    46  					t.Errorf("unexpected values for S: want [0x0, 0xFFFF] got:%f", hsva.S)
    47  				}
    48  				if cFromHSVB > 0xFFFF {
    49  					t.Errorf("unexpected values for V: want [0x0, 0xFFFF] got:%f", hsva.V)
    50  				}
    51  				if cFromHSVA > 0xFFFF {
    52  					t.Errorf("unexpected values for V: want [0x0, 0xFFFF] got:%f", hsva.V)
    53  				}
    54  
    55  				back := rgbaToHsva(color.RGBA{uint8(cFromHSVR >> 8), uint8(cFromHSVG >> 8), uint8(cFromHSVB >> 8), uint8(cFromHSVA)}.RGBA())
    56  				if hsva != back {
    57  					t.Errorf("roundtrip error: got:%#v want:%#v", back, hsva)
    58  				}
    59  				const epsilon = 1
    60  				if !withinEpsilon(cFromHSVR, cDirectR, epsilon) {
    61  					t.Errorf("roundtrip error for R: got:%d want:%d", cFromHSVR, cDirectR)
    62  				}
    63  				if !withinEpsilon(cFromHSVG, cDirectG, epsilon) {
    64  					t.Errorf("roundtrip error for G: got:%d want:%d %d", cFromHSVG, cDirectG, cFromHSVG-cDirectG)
    65  				}
    66  				if !withinEpsilon(cFromHSVB, cDirectB, epsilon) {
    67  					t.Errorf("roundtrip error for B: got:%d want:%d", cFromHSVB, cDirectB)
    68  				}
    69  				if cFromHSVA != cDirectA {
    70  					t.Errorf("roundtrip error for A: got:%d want:%d", cFromHSVA, cDirectA)
    71  				}
    72  				if t.Failed() {
    73  					return
    74  				}
    75  			}
    76  		}
    77  	}
    78  }
    79  

View as plain text