# How do I calculate average hue, since the numbers loop around?

If I calculate the average (mean) hue of a set of colours naively, I will get silly answers sometimes.

For example, in HSB/HSL, the average of hue=10 (slightly orange red) and hue=350 (slightly blue red) would arithmetically be hue=(10+350)/2=180 (cyan).

Considering the hue channel only, how should I calculate averages in order to get results which match intuition in these cases?

• Do 350-10=x and 360-350+10=y to get the change in degree if you go clockwise or counter clockwise. Then do 10+x/2 or (10-y/2)%360 depending on which is closest. This assumes your first degree is smaller than the second Commented Aug 29, 2018 at 16:34
• – user106
Commented Aug 30, 2018 at 3:55
• @Rahul Want to post that as an answer? :) Commented Aug 30, 2018 at 17:17

How do I calculate average hue, since the numbers loop around?

I will get silly answers sometimes. For example, in HSB/HSL, the average of hue=10 (slightly orange red) and hue=350 (slightly blue red) would arithmetically be hue=(10+350)/2=180 (cyan).

What if the values were 0° and 180°, then there are two answers: 90° and 270°; the same thing applies whenever the two inputs are opposite, there's two answers and no clear choice.

• The program "Image Color Summarizer" (Linux version) has a file called "peravg.pm" which says: "compute the average and median of a periodic data set, like a set of angles or hues". The main page offers a Windows version and a webpage image upload along with an API.

The author explains:

The colour summarizer will produce descriptive colour statistics for an image. Reported will be the average, median, minimum and maximum of each component of RGB, HSV, LCH and Lab. Average hues are calculated using mean of circular quantities.

Some of the questions the summarizer will answer are:

• what is the average color hue, saturation and value in my image?
• what are the colours that are most representative of the image?
• what is the image's human readable colour description (e.g. dark pure blue)?
• what do the RGB, HSV, LCH and Lab histograms look like?
• what are the RGB, HSV, LCH and Lab pixel values?

The paper: "Perceiving the average hue of color arrays" (May 1 2014), by Jacquelyn Webster, Paul Kay, and Michael A. Webster examines the subject from the viewpoint of an observer's perception of color.