TheAlgorithms/Java · error · IllegalArgumentException
hue should be between 0 and 360
Error message
hue should be between 0 and 360
What it means
Thrown by RgbHsvConversion.hsvToRgb when the hue parameter is less than 0 or greater than 360. Hue is measured in degrees on the color wheel and the valid range is [0, 360]; values outside this range are geometrically meaningless for the hue-section calculation (hue / 60).
Source
Thrown at src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java:60
assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[] {120, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[] {240, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[] {300, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[] {180, 0.5, 0.5});
assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88});
assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5});
}
/**
* Conversion from the HSV-representation to the RGB-representation.
*
* @param hue Hue of the color.
* @param saturation Saturation of the color.
* @param value Brightness-value of the color.
* @return The tuple of RGB-components.
*/
public static int[] hsvToRgb(double hue, double saturation, double value) {
if (hue < 0 || hue > 360) {
throw new IllegalArgumentException("hue should be between 0 and 360");
}
if (saturation < 0 || saturation > 1) {
throw new IllegalArgumentException("saturation should be between 0 and 1");
}
if (value < 0 || value > 1) {
throw new IllegalArgumentException("value should be between 0 and 1");
}
double chroma = value * saturation;
double hueSection = hue / 60;
double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));
double matchValue = value - chroma;
return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Clamp hue to [0, 360] using Math.max(0, Math.min(360, hue)) before calling hsvToRgb.
- Normalize hue with modulo: hue = ((hue % 360) + 360) % 360, which wraps out-of-range values into the valid interval.
- Validate at the UI boundary that the slider or input field constrains hue to [0, 360].
Example fix
// before int[] rgb = RgbHsvConversion.hsvToRgb(hue, sat, val); // after hue = ((hue % 360) + 360) % 360; // wrap into [0, 360) int[] rgb = RgbHsvConversion.hsvToRgb(hue, sat, val);
Defensive patterns
Strategy: validation
Validate before calling
hue = ((hue % 360) + 360) % 360; // wrap into [0, 360) int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);
Type guard
static boolean isValidHue(double hue) {
return hue >= 0 && hue <= 360;
} Try / catch
try {
int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);
} catch (IllegalArgumentException e) {
// hue out of range; normalize and retry
hue = ((hue % 360) + 360) % 360;
int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);
} Prevention
- Wrap hue with modulo ((hue % 360) + 360) % 360 to handle both negative and overflow values.
- Constrain UI sliders and inputs to [0, 360] at the presentation layer.
- Document that hue is in degrees, not radians or percentage.
When it happens
Trigger: Passing a negative hue value or a hue greater than 360. Supplying hue from a UI color picker that allows out-of-range values. Computing hue from a formula that can produce values outside [0, 360] (e.g., an unnormalized arctangent result).
Common situations: A color picker slider is configured without min/max constraints. Hue is computed from image processing code that can produce values slightly outside the expected range. User-entered HSV values are passed through without clamping.
Related errors
- saturation should be between 0 and 1
- value should be between 0 and 1
- red should be between 0 and 255
- green should be between 0 and 255
- blue should be between 0 and 255
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d88b007adfe501cc.
Report an issue: GitHub.