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?
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).


How to calculate mean and standard deviation for hue values from 0 to 360?
 and Averaging circular values (particularly Hues in HSL color scheme) which provides C++ source code.

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 &quot;Image Color Summarizer&quot; (Linux version) has a file called &quot;peravg.pm&quot; which says: &quot;compute the average and median of a periodic data set, like a set of angles or hues&quot;. 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: &quot;Perceiving the average hue of color arrays&quot; (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.