1package colorful 2 3import ( 4 "fmt" 5 "testing" 6) 7 8// This is really difficult to test, if you've got a good idea, pull request! 9 10// Check if it returns all valid and enough colors. 11func TestColorCount(t *testing.T) { 12 fmt.Printf("Testing up to %v palettes may take a while...\n", 100) 13 for i := 0; i < 100; i++ { 14 //pal, err := SoftPaletteEx(i, SoftPaletteGenSettings{nil, 50, true}) 15 pal, err := SoftPalette(i) 16 if err != nil { 17 t.Errorf("Error: %v", err) 18 } 19 20 // Check the color count of the palette 21 if len(pal) != i { 22 t.Errorf("Requested %v colors but got %v", i, len(pal)) 23 } 24 25 // Also check whether all colors exist in RGB space. 26 for icol, col := range pal { 27 if !col.IsValid() { 28 t.Errorf("Color %v in palette of %v is invalid: %v", icol, len(pal), col) 29 } 30 } 31 } 32 fmt.Println("Done with that, but more tests to run.") 33} 34 35// Check if it errors-out on an impossible constraint 36func TestImpossibleConstraint(t *testing.T) { 37 never := func(l, a, b float64) bool { return false } 38 39 pal, err := SoftPaletteEx(10, SoftPaletteSettings{never, 50, true}) 40 if err == nil || pal != nil { 41 t.Error("Should error-out on impossible constraint!") 42 } 43} 44 45// Check whether the constraint is respected 46func TestConstraint(t *testing.T) { 47 octant := func(l, a, b float64) bool { return l <= 0.5 && a <= 0.0 && b <= 0.0 } 48 49 pal, err := SoftPaletteEx(100, SoftPaletteSettings{octant, 50, true}) 50 if err != nil { 51 t.Errorf("Error: %v", err) 52 } 53 54 // Check ALL the colors! 55 for icol, col := range pal { 56 if !col.IsValid() { 57 t.Errorf("Color %v in constrained palette is invalid: %v", icol, col) 58 } 59 60 l, a, b := col.Lab() 61 if l > 0.5 || a > 0.0 || b > 0.0 { 62 t.Errorf("Color %v in constrained palette violates the constraint: %v (lab: %v)", icol, col, [3]float64{l, a, b}) 63 } 64 } 65} 66