Skip to content

Commit

Permalink
Remove shift limitation
Browse files Browse the repository at this point in the history
Removes the [-180,180] limitation from the hue shift and adds additional test cases for -540, -360, -350, 350, 360, 540, -480 (golden), 480 (golden).
  • Loading branch information
struffel committed Dec 18, 2020
1 parent 0d8eac9 commit 3956733
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 10 deletions.
10 changes: 6 additions & 4 deletions adjust.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
}

// AdjustHue changes the hue of the image using the shift parameter (measured in degrees) and returns the adjusted image.
// The shift must be in the range (-180, 180).
// The shift = 0 gives the original image.
// The shift = 0 (or 360 / -360 / etc.) gives the original image.
// The shift = 180 (or -180) corresponds to a 180° degree rotation of the color wheel and thus gives the image with its hue inverted for each pixel.
//
// Examples:
Expand All @@ -94,13 +93,16 @@ func AdjustHue(img image.Image, shift float64) *image.NRGBA {
return Clone(img)
}

shift = math.Min(math.Max(shift, -180), 180)
summand := shift / 360

return AdjustFunc(img, func(c color.NRGBA) color.NRGBA {
h, s, l := rgbToHSL(c.R, c.G, c.B)
h += summand
h = math.Mod(h, 1.0)
h = math.Mod(h, 1)
//Adding 1 because Golang's Modulo function behaves differently to similar operators in most other languages.
if h < 0 {
h++
}
r, g, b := hslToRGB(h, s, l)
return color.NRGBA{r, g, b, c.A}
})
Expand Down
146 changes: 140 additions & 6 deletions adjust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestAdjustHue(t *testing.T) {
want *image.NRGBA
}{
{
"AdjustHue 3x3 -10",
"AdjustHue 3x3 -540",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Expand All @@ -265,13 +265,57 @@ func TestAdjustHue(t *testing.T) {
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
-10,
-540,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x22, 0x01, 0x22, 0xcc, 0x00, 0x02, 0x00, 0x22, 0xcc, 0x03,
0x11, 0x28, 0x33, 0xff, 0x33, 0x1c, 0x11, 0xff, 0x93, 0x33, 0xbb, 0xff,
0x00, 0xcc, 0xcc, 0x01, 0xcc, 0x00, 0xcc, 0x02, 0xcc, 0xcc, 0x00, 0x03,
0x33, 0x22, 0x11, 0xff, 0x11, 0x22, 0x33, 0xff, 0x44, 0xbb, 0x33, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
},
{
"AdjustHue 3x3 -360",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
-360,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
},
{
"AdjustHue 3x3 -350",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
-350,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x22, 0x00, 0x01, 0x00, 0xcc, 0x22, 0x02, 0x22, 0x00, 0xcc, 0x03,
0x11, 0x1c, 0x33, 0xff, 0x33, 0x28, 0x11, 0xff, 0xbb, 0x33, 0xb5, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
Expand All @@ -298,6 +342,50 @@ func TestAdjustHue(t *testing.T) {
},
},
},
{
"AdjustHue 3x3 -10",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
-10,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x22, 0x01, 0x22, 0xcc, 0x00, 0x02, 0x00, 0x22, 0xcc, 0x03,
0x11, 0x28, 0x33, 0xff, 0x33, 0x1c, 0x11, 0xff, 0x93, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
},
{
"AdjustHue 3x3 0",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
0,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
},
{
"AdjustHue 3x3 10",
&image.NRGBA{
Expand Down Expand Up @@ -343,7 +431,7 @@ func TestAdjustHue(t *testing.T) {
},
},
{
"AdjustHue 3x3 0",
"AdjustHue 3x3 350",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Expand All @@ -353,7 +441,29 @@ func TestAdjustHue(t *testing.T) {
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
0,
350,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x22, 0x01, 0x22, 0xcc, 0x00, 0x02, 0x00, 0x22, 0xcc, 0x03,
0x11, 0x28, 0x33, 0xff, 0x33, 0x1c, 0x11, 0xff, 0x93, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
},
{
"AdjustHue 3x3 360",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
360,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Expand All @@ -364,6 +474,28 @@ func TestAdjustHue(t *testing.T) {
},
},
},
{
"AdjustHue 3x3 540",
&image.NRGBA{
Rect: image.Rect(-1, -1, 2, 2),
Stride: 3 * 4,
Pix: []uint8{
0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
540,
&image.NRGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0x00, 0xcc, 0xcc, 0x01, 0xcc, 0x00, 0xcc, 0x02, 0xcc, 0xcc, 0x00, 0x03,
0x33, 0x22, 0x11, 0xff, 0x11, 0x22, 0x33, 0xff, 0x44, 0xbb, 0x33, 0xff,
0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -377,10 +509,12 @@ func TestAdjustHue(t *testing.T) {

func TestAdjustHueGolden(t *testing.T) {
for name, p := range map[string]float64{
"out_hue_m480.png": -480,
"out_hue_m120.png": -120,
"out_hue_m60.png": -60,
"out_hue_p60.png": 60,
"out_hue_p120.png": 120,
"out_hue_p480.png": 480,
} {
got := AdjustHue(testdataFlowersSmallPNG, p)
want, err := Open("testdata/" + name)
Expand Down
Binary file added testdata/out_hue_m480.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/out_hue_p480.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3956733

Please sign in to comment.