Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion adjust/adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func Contrast(src image.Image, change float64) *image.RGBA {
func Hue(img image.Image, change int) *image.RGBA {
fn := func(c color.RGBA) color.RGBA {
h, s, l := util.RGBToHSL(c)
h = float64((int(h) + change) % 360)
// Go's % keeps the sign of the dividend, so a negative rotation needs re-normalizing
h = float64(((int(h)+change)%360 + 360) % 360)
outColor := util.HSLToRGB(h, s, l)
outColor.A = c.A
return outColor
Expand Down
30 changes: 30 additions & 0 deletions adjust/adjustment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,33 @@ func TestHue(t *testing.T) {
}
}
}

func TestHueFullRotation(t *testing.T) {
// Desaturated samples: a fully saturated pixel hides the wrap-around error
// because the out-of-range channel clamps back onto the correct value.
src := &image.RGBA{
Rect: image.Rect(0, 0, 2, 2),
Stride: 8,
Pix: []uint8{
0xC0, 0x80, 0x40, 0xFF, 0x80, 0x40, 0xC0, 0xFF,
0x90, 0x70, 0x50, 0x80, 0x40, 0xC0, 0x80, 0xFF,
},
}

cases := []struct {
name string
change int
}{
{"positive", 360},
{"negative", -360},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := Hue(src, c.change)
if !util.RGBAImageEqual(actual, src) {
t.Errorf("Hue(src, %d):\nexpected: %v\nactual: %v", c.change, util.RGBAToString(src), util.RGBAToString(actual))
}
})
}
}