...

Source file src/github.com/lucasb-eyer/go-colorful/hsluv_test.go

Documentation: github.com/lucasb-eyer/go-colorful

     1  package colorful
     2  
     3  // Source: https://github.com/hsluv/hsluv-go
     4  // Under MIT License
     5  // Modified so that Saturation and Luminance are in [0..1] instead of [0..100],
     6  // and so that it works with this library in general.
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"math"
    12  	"os"
    13  	"testing"
    14  )
    15  
    16  type mapping map[string]values
    17  
    18  type values struct {
    19  	Rgb   [3]float64
    20  	Xyz   [3]float64
    21  	Luv   [3]float64
    22  	Lch   [3]float64
    23  	Hsluv [3]float64
    24  	Hpluv [3]float64
    25  }
    26  
    27  func pack(a, b, c float64) [3]float64 {
    28  	return [3]float64{a, b, c}
    29  }
    30  
    31  func unpack(tuple [3]float64) (float64, float64, float64) {
    32  	return tuple[0], tuple[1], tuple[2]
    33  }
    34  
    35  func fromHex(s string) Color {
    36  	c, _ := Hex(s)
    37  	return c
    38  }
    39  
    40  // const delta = 0.00000001
    41  const hsluvTestDelta = 0.0000000001 // Two more zeros than the original delta, because values are divided by 100
    42  
    43  func compareTuple(t *testing.T, result, expected [3]float64, method string, hex string) {
    44  	var err bool
    45  	var errs [3]bool
    46  	for i := 0; i < 3; i++ {
    47  		if math.Abs(result[i]-expected[i]) > hsluvTestDelta {
    48  			err = true
    49  			errs[i] = true
    50  		}
    51  	}
    52  	if err {
    53  		resultOutput := "["
    54  		for i := 0; i < 3; i++ {
    55  			resultOutput += fmt.Sprintf("%f", result[i])
    56  			if errs[i] {
    57  				resultOutput += " *"
    58  			}
    59  			if i < 2 {
    60  				resultOutput += ", "
    61  			}
    62  		}
    63  		resultOutput += "]"
    64  		t.Errorf("result: %s expected: %v, testing %s with test case %s", resultOutput, expected, method, hex)
    65  	}
    66  }
    67  
    68  func compareHex(t *testing.T, result, expected string, method string, hex string) {
    69  	if result != expected {
    70  		t.Errorf("result: %v expected: %v, testing %s with test case %s", result, expected, method, hex)
    71  	}
    72  }
    73  
    74  func TestHSLuv(t *testing.T) {
    75  	snapshotFile, err := os.Open("hsluv-snapshot-rev4.json")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	defer snapshotFile.Close()
    80  
    81  	jsonParser := json.NewDecoder(snapshotFile)
    82  	snapshot := make(mapping)
    83  	if err = jsonParser.Decode(&snapshot); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	for hex, colorValues := range snapshot {
    88  		// tests for public methods
    89  		if testing.Verbose() {
    90  			t.Logf("Testing public methods for test case %s", hex)
    91  		}
    92  
    93  		// Adjust color values to be in the ranges this library uses
    94  		colorValues.Hsluv[1] /= 100.0
    95  		colorValues.Hsluv[2] /= 100.0
    96  		colorValues.Hpluv[1] /= 100.0
    97  		colorValues.Hpluv[2] /= 100.0
    98  
    99  		compareHex(t, HSLuv(unpack(colorValues.Hsluv)).Hex(), hex, "HsluvToHex", hex)
   100  		compareTuple(t, pack(HSLuv(unpack(colorValues.Hsluv)).values()), colorValues.Rgb, "HsluvToRGB", hex)
   101  		compareTuple(t, pack(fromHex(hex).HSLuv()), colorValues.Hsluv, "HsluvFromHex", hex)
   102  		compareTuple(t, pack(Color{colorValues.Rgb[0], colorValues.Rgb[1], colorValues.Rgb[2]}.HSLuv()), colorValues.Hsluv, "HsluvFromRGB", hex)
   103  		compareHex(t, HPLuv(unpack(colorValues.Hpluv)).Hex(), hex, "HpluvToHex", hex)
   104  		compareTuple(t, pack(HPLuv(unpack(colorValues.Hpluv)).values()), colorValues.Rgb, "HpluvToRGB", hex)
   105  		compareTuple(t, pack(fromHex(hex).HPLuv()), colorValues.Hpluv, "HpluvFromHex", hex)
   106  		compareTuple(t, pack(Color{colorValues.Rgb[0], colorValues.Rgb[1], colorValues.Rgb[2]}.HPLuv()), colorValues.Hpluv, "HpluvFromRGB", hex)
   107  
   108  		if !testing.Short() {
   109  			// internal tests
   110  			if testing.Verbose() {
   111  				t.Logf("Testing internal methods for test case %s", hex)
   112  			}
   113  
   114  			// Adjust color values to be in the ranges this library uses
   115  			colorValues.Lch[0] /= 100.0
   116  			colorValues.Lch[1] /= 100.0
   117  			colorValues.Luv[0] /= 100.0
   118  			colorValues.Luv[1] /= 100.0
   119  			colorValues.Luv[2] /= 100.0
   120  
   121  			compareTuple(t, pack(LuvLChWhiteRef(
   122  				colorValues.Lch[0], colorValues.Lch[1], colorValues.Lch[2], hSLuvD65,
   123  			).values()), colorValues.Rgb, "convLchRgb", hex)
   124  			compareTuple(t, pack(Color{
   125  				colorValues.Rgb[0], colorValues.Rgb[1], colorValues.Rgb[2],
   126  			}.LuvLChWhiteRef(hSLuvD65)), colorValues.Lch, "convRgbLch", hex)
   127  			compareTuple(t, pack(XyzToLuvWhiteRef(
   128  				colorValues.Xyz[0], colorValues.Xyz[1], colorValues.Xyz[2], hSLuvD65,
   129  			)), colorValues.Luv, "convXyzLuv", hex)
   130  			compareTuple(t, pack(LuvToXyzWhiteRef(
   131  				colorValues.Luv[0], colorValues.Luv[1], colorValues.Luv[2], hSLuvD65,
   132  			)), colorValues.Xyz, "convLuvXyz", hex)
   133  			compareTuple(t, pack(LuvToLuvLCh(unpack(colorValues.Luv))), colorValues.Lch, "convLuvLch", hex)
   134  			compareTuple(t, pack(LuvLChToLuv(unpack(colorValues.Lch))), colorValues.Luv, "convLchLuv", hex)
   135  			compareTuple(t, pack(HSLuvToLuvLCh(unpack(colorValues.Hsluv))), colorValues.Lch, "convHsluvLch", hex)
   136  			compareTuple(t, pack(LuvLChToHSLuv(unpack(colorValues.Lch))), colorValues.Hsluv, "convLchHsluv", hex)
   137  			compareTuple(t, pack(HPLuvToLuvLCh(unpack(colorValues.Hpluv))), colorValues.Lch, "convHpluvLch", hex)
   138  			compareTuple(t, pack(LuvLChToHPLuv(unpack(colorValues.Lch))), colorValues.Hpluv, "convLchHpluv", hex)
   139  			compareTuple(t, pack(LinearRgb(XyzToLinearRgb(unpack(colorValues.Xyz))).values()), colorValues.Rgb, "convXyzRgb", hex)
   140  			compareTuple(t, pack(Color{colorValues.Rgb[0], colorValues.Rgb[1], colorValues.Rgb[2]}.Xyz()), colorValues.Xyz, "convRgbXyz", hex)
   141  		}
   142  	}
   143  }
   144  

View as plain text