# HSV Color Model
HSV (Hue, Saturation, Value) is a cylindrical color model that represents colors in a way that corresponds more closely to human perception than the RGB model. By separating color information (hue and saturation) from brightness information (value), HSV provides an intuitive framework for color selection, manipulation, and analysis in digital design, image processing, and computer vision applications.
## Understanding HSV Components
### Hue (H) - The Color Identity
Hue represents the pure color type and is measured as an angle around the color cylinder:
- **0° (360°)**: Red
- **60°**: Yellow
- **120°**: Green
- **180°**: Cyan
- **240°**: Blue
- **300°**: Magenta
```javascript
// HSV hue values for primary and secondary colors
const hueValues = {
red: 0,
orange: 30,
yellow: 60,
chartreuse: 90,
green: 120,
springGreen: 150,
cyan: 180,
azure: 210,
blue: 240,
violet: 270,
magenta: 300,
rose: 330
};
```
### Saturation (S) - Color Intensity
Saturation controls the purity or intensity of the color, expressed as a percentage:
- **0%**: Completely desaturated (grayscale)
- **50%**: Moderately saturated
- **100%**: Fully saturated (pure, vivid color)
```javascript
// Saturation examples for red hue
const redVariations = {
gray: { h: 0, s: 0, v: 50 }, // No color, pure gray
dustyRose: { h: 0, s: 25, v: 75 }, // Muted red
coral: { h: 0, s: 50, v: 85 }, // Medium saturation
pureRed: { h: 0, s: 100, v: 100 } // Maximum saturation
};
```
### Value (V) - Brightness Level
Value represents the brightness or luminosity of the color:
- **0%**: Black (no brightness)
- **50%**: Medium brightness
- **100%**: Maximum brightness
```javascript
// Value examples for pure blue
const blueShades = {
black: { h: 240, s: 100, v: 0 }, // No brightness
darkBlue: { h: 240, s: 100, v: 25 }, // Dark
mediumBlue: { h: 240, s: 100, v: 50 }, // Medium
brightBlue: { h: 240, s: 100, v: 100 } // Maximum brightness
};
```
## HSV Color Space Geometry
### Cylindrical Representation
HSV is visualized as a cylinder where:
- **Circumference**: Represents hue (0-360°)
- **Radius**: Represents saturation (0-100%)
- **Height**: Represents value (0-100%)
```javascript
// Convert HSV to cylindrical coordinates
function hsvToCylindrical(h, s, v) {
const hueRadians = (h * Math.PI) / 180;
const radius = s / 100;
const height = v / 100;
return {
x: radius * Math.cos(hueRadians),
y: radius * Math.sin(hueRadians),
z: height,
angle: h,
radius: radius,
height: height
};
}
```
## RGB to HSV Conversion
### Mathematical Conversion Algorithm
```javascript
function rgbToHsv(r, g, b) {
// Normalize RGB values to 0-1 range
r = r / 255;
g = g / 255;
b = b / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const delta = max - min;
// Calculate Value (brightness)
const v = max;
// Calculate Saturation
const s = max === 0 ? 0 : delta / max;
// Calculate Hue
let h = 0;
if (delta !== 0) {
if (max === r) {
h = ((g - b) / delta) % 6;
} else if (max === g) {
h = (b - r) / delta + 2;
} else {
h = (r - g) / delta + 4;
}
h *= 60;
if (h < 0) h += 360;
}
return {
h: Math.round(h),
s: Math.round(s * 100),
v: Math.round(v * 100)
};
}
// Example usage
const rgbColor = { r: 255, g: 128, b: 0 }; // Orange
const hsvColor = rgbToHsv(rgbColor.r, rgbColor.g, rgbColor.b);
console.log(hsvColor); // { h: 30, s: 100, v: 100 }
```
## HSV to RGB Conversion
### Reverse Conversion Algorithm
```javascript
function hsvToRgb(h, s, v) {
// Normalize inputs
h = h / 360;
s = s / 100;
v = v / 100;
const c = v * s; // Chroma
const x = c * (1 - Math.abs(((h * 6) % 2) - 1));
const m = v - c;
let r, g, b;
if (h < 1/6) {
[r, g, b] = [c, x, 0];
} else if (h < 2/6) {
[r, g, b] = [x, c, 0];
} else if (h < 3/6) {
[r, g, b] = [0, c, x];
} else if (h < 4/6) {
[r, g, b] = [0, x, c];
} else if (h < 5/6) {
[r, g, b] = [x, 0, c];
} else {
[r, g, b] = [c, 0, x];
}
return {
r: Math.round((r + m) * 255),
g: Math.round((g + m) * 255),
b: Math.round((b + m) * 255)
};
}
// Example usage
const hsvOrange = { h: 30, s: 100, v: 100 };
const rgbOrange = hsvToRgb(hsvOrange.h, hsvOrange.s, hsvOrange.v);
console.log(rgbOrange); // { r: 255, g: 128, b: 0 }
```
## HSV vs HSL Comparison
### Key Differences
```javascript
// Compare HSV and HSL for the same RGB color
function compareHsvHsl(r, g, b) {
const hsv = rgbToHsv(r, g, b);
const hsl = rgbToHsl(r, g, b); // Assuming rgbToHsl function exists
return {
rgb: { r, g, b },
hsv: hsv,
hsl: hsl,
differences: {
hue: Math.abs(hsv.h - hsl.h),
saturation: Math.abs(hsv.s - hsl.s),
brightness: Math.abs(hsv.v - hsl.l)
}
};
}
// Example comparison
const comparison = compareHsvHsl(128, 64, 192);
console.log('HSV vs HSL comparison:', comparison);
```
### When to Use HSV vs HSL
- **HSV**: Better for color picking interfaces, image processing, computer vision
- **HSL**: Better for web development, CSS, design systems
- **HSV Value**: Represents maximum RGB component
- **HSL Lightness**: Represents average of max and min RGB components
## Color Manipulation with HSV
### Creating Color Variations
```javascript
class HSVColorManipulator {
constructor(h, s, v) {
this.h = h;
this.s = s;
this.v = v;
}
// Create lighter version by increasing value
lighten(amount = 10) {
return new HSVColorManipulator(
this.h,
this.s,
Math.min(100, this.v + amount)
);
}
// Create darker version by decreasing value
darken(amount = 10) {
return new HSVColorManipulator(
this.h,
this.s,
Math.max(0, this.v - amount)
);
}
// Create more saturated version
saturate(amount = 10) {
return new HSVColorManipulator(
this.h,
Math.min(100, this.s + amount),
this.v
);
}
// Create less saturated version
desaturate(amount = 10) {
return new HSVColorManipulator(
this.h,
Math.max(0, this.s - amount),
this.v
);
}
// Rotate hue for color harmony
rotateHue(degrees) {
return new HSVColorManipulator(
(this.h + degrees + 360) % 360,
this.s,
this.v
);
}
// Get complementary color
complement() {
return this.rotateHue(180);
}
// Get triadic colors
triadic() {
return [
this,
this.rotateHue(120),
this.rotateHue(240)
];
}
// Get analogous colors
analogous(angle = 30) {
return [
this.rotateHue(-angle),
this,
this.rotateHue(angle)
];
}
// Convert to RGB
toRgb() {
return hsvToRgb(this.h, this.s, this.v);
}
// Convert to hex
toHex() {
const rgb = this.toRgb();
return `#${rgb.r.toString(16).padStart(2, '0')}${rgb.g.toString(16).padStart(2, '0')}${rgb.b.toString(16).padStart(2, '0')}`;
}
}
// Example usage
const baseColor = new HSVColorManipulator(240, 100, 75); // Blue
const palette = {
base: baseColor.toHex(),
light: baseColor.lighten(20).toHex(),
dark: baseColor.darken(20).toHex(),
muted: baseColor.desaturate(30).toHex(),
complement: baseColor.complement().toHex()
};
console.log('Color palette:', palette);
```
## Image Processing Applications
### Color-Based Image Segmentation
```javascript
// HSV-based color range detection
function createColorMask(imageData, targetHsv, tolerance) {
const mask = new Uint8Array(imageData.data.length / 4);
for (let i = 0; i < imageData.data.length; i += 4) {
const r = imageData.data[i];
const g = imageData.data[i + 1];
const b = imageData.data[i + 2];
const hsv = rgbToHsv(r, g, b);
// Check if pixel is within tolerance range
const hueDistance = Math.min(
Math.abs(hsv.h - targetHsv.h),
360 - Math.abs(hsv.h - targetHsv.h)
);
const inRange = (
hueDistance <= tolerance.h &&
Math.abs(hsv.s - targetHsv.s) <= tolerance.s &&
Math.abs(hsv.v - targetHsv.v) <= tolerance.v
);
mask[i / 4] = inRange ? 255 : 0;
}
return mask;
}
// Example: Detect blue objects in image
const blueTarget = { h: 240, s: 80, v: 70 };
const tolerance = { h: 20, s: 30, v: 30 };
// const blueMask = createColorMask(imageData, blueTarget, tolerance);
```
### Histogram Analysis
```javascript
// Generate HSV histogram
function generateHSVHistogram(imageData) {
const histogram = {
hue: new Array(360).fill(0),
saturation: new Array(101).fill(0),
value: new Array(101).fill(0)
};
for (let i = 0; i < imageData.data.length; i += 4) {
const r = imageData.data[i];
const g = imageData.data[i + 1];
const b = imageData.data[i + 2];
const hsv = rgbToHsv(r, g, b);
histogram.hue[Math.floor(hsv.h)]++;
histogram.saturation[Math.floor(hsv.s)]++;
histogram.value[Math.floor(hsv.v)]++;
}
return histogram;
}
```
## Color Harmony and Theory
### Systematic Color Scheme Generation
```javascript
class HSVColorSchemes {
static monochromatic(baseHsv, steps = 5) {
const colors = [];
const valueStep = 100 / (steps - 1);
for (let i = 0; i < steps; i++) {
colors.push({
h: baseHsv.h,
s: baseHsv.s,
v: Math.round(i * valueStep)
});
}
return colors;
}
static analogous(baseHsv, count = 5, spread = 30) {
const colors = [];
const startHue = baseHsv.h - (spread * (count - 1)) / 2;
for (let i = 0; i < count; i++) {
colors.push({
h: (startHue + i * spread + 360) % 360,
s: baseHsv.s,
v: baseHsv.v
});
}
return colors;
}
static complementary(baseHsv) {
return [
baseHsv,
{
h: (baseHsv.h + 180) % 360,
s: baseHsv.s,
v: baseHsv.v
}
];
}
static triadic(baseHsv) {
return [
baseHsv,
{
h: (baseHsv.h + 120) % 360,
s: baseHsv.s,
v: baseHsv.v
},
{
h: (baseHsv.h + 240) % 360,
s: baseHsv.s,
v: baseHsv.v
}
];
}
static tetradic(baseHsv) {
return [
baseHsv,
{
h: (baseHsv.h + 90) % 360,
s: baseHsv.s,
v: baseHsv.v
},
{
h: (baseHsv.h + 180) % 360,
s: baseHsv.s,
v: baseHsv.v
},
{
h: (baseHsv.h + 270) % 360,
s: baseHsv.s,
v: baseHsv.v
}
];
}
}
// Example usage
const baseColor = { h: 210, s: 75, v: 85 };
const schemes = {
monochromatic: HSVColorSchemes.monochromatic(baseColor),
analogous: HSVColorSchemes.analogous(baseColor),
complementary: HSVColorSchemes.complementary(baseColor),
triadic: HSVColorSchemes.triadic(baseColor),
tetradic: HSVColorSchemes.tetradic(baseColor)
};
```
## Computer Vision Applications
### Object Tracking by Color
```javascript
// Track objects based on HSV color range
class HSVColorTracker {
constructor(targetHsv, tolerance) {
this.target = targetHsv;
this.tolerance = tolerance;
}
findColorRegions(imageData) {
const width = imageData.width;
const height = imageData.height;
const regions = [];
const visited = new Set();
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const index = (y * width + x) * 4;
const pixelId = y * width + x;
if (visited.has(pixelId)) continue;
const r = imageData.data[index];
const g = imageData.data[index + 1];
const b = imageData.data[index + 2];
const hsv = rgbToHsv(r, g, b);
if (this.isColorMatch(hsv)) {
const region = this.floodFill(imageData, x, y, visited);
if (region.pixels.length > 100) { // Minimum region size
regions.push(region);
}
}
}
}
return regions;
}
isColorMatch(hsv) {
const hueDistance = Math.min(
Math.abs(hsv.h - this.target.h),
360 - Math.abs(hsv.h - this.target.h)
);
return (
hueDistance <= this.tolerance.h &&
Math.abs(hsv.s - this.target.s) <= this.tolerance.s &&
Math.abs(hsv.v - this.target.v) <= this.tolerance.v
);
}
floodFill(imageData, startX, startY, visited) {
const width = imageData.width;
const height = imageData.height;
const stack = [{ x: startX, y: startY }];
const pixels = [];
let minX = startX, maxX = startX;
let minY = startY, maxY = startY;
while (stack.length > 0) {
const { x, y } = stack.pop();
const pixelId = y * width + x;
if (visited.has(pixelId) || x < 0 || x >= width || y < 0 || y >= height) {
continue;
}
const index = (y * width + x) * 4;
const r = imageData.data[index];
const g = imageData.data[index + 1];
const b = imageData.data[index + 2];
const hsv = rgbToHsv(r, g, b);
if (!this.isColorMatch(hsv)) continue;
visited.add(pixelId);
pixels.push({ x, y, hsv });
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
// Add neighboring pixels
stack.push(
{ x: x + 1, y },
{ x: x - 1, y },
{ x, y: y + 1 },
{ x, y: y - 1 }
);
}
return {
pixels,
boundingBox: { minX, maxX, minY, maxY },
center: {
x: (minX + maxX) / 2,
y: (minY + maxY) / 2
},
area: pixels.length
};
}
}
```
## Performance Optimization
### Efficient HSV Operations
```javascript
// Optimized HSV operations for real-time applications
class OptimizedHSV {
// Pre-computed lookup tables for faster conversion
static createHueLookupTable() {
const table = new Float32Array(256 * 256 * 256);
for (let r = 0; r < 256; r++) {
for (let g = 0; g < 256; g++) {
for (let b = 0; b < 256; b++) {
const index = (r << 16) | (g << 8) | b;
const hsv = rgbToHsv(r, g, b);
table[index] = hsv.h;
}
}
}
return table;
}
// Batch conversion for image processing
static batchRgbToHsv(rgbArray) {
const hsvArray = new Float32Array(rgbArray.length);
for (let i = 0; i < rgbArray.length; i += 3) {
const hsv = rgbToHsv(rgbArray[i], rgbArray[i + 1], rgbArray[i + 2]);
hsvArray[i] = hsv.h;
hsvArray[i + 1] = hsv.s;
hsvArray[i + 2] = hsv.v;
}
return hsvArray;
}
}
```
## Accessibility and HSV
### Color Contrast Analysis
```javascript
// Analyze color contrast using HSV
function analyzeHSVContrast(hsv1, hsv2) {
const rgb1 = hsvToRgb(hsv1.h, hsv1.s, hsv1.v);
const rgb2 = hsvToRgb(hsv2.h, hsv2.s, hsv2.v);
// Calculate relative luminance
const luminance1 = calculateLuminance(rgb1);
const luminance2 = calculateLuminance(rgb2);
const contrastRatio = (Math.max(luminance1, luminance2) + 0.05) /
(Math.min(luminance1, luminance2) + 0.05);
return {
ratio: contrastRatio,
wcagAA: contrastRatio >= 4.5,
wcagAAA: contrastRatio >= 7,
valueDifference: Math.abs(hsv1.v - hsv2.v),
saturationDifference: Math.abs(hsv1.s - hsv2.s)
};
}
function calculateLuminance(rgb) {
const [r, g, b] = [rgb.r, rgb.g, rgb.b].map(c => {
c = c / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
```
## Related Concepts
- **[HSL Color Space](/glossary/hsl)**: Alternative perceptual color model
- **[RGB Color Space](/glossary/rgb-color-space)**: Additive color model for displays
- **[Color Theory](/glossary/color-theory)**: Principles of color relationships
- **[Color Harmony](/glossary/color-harmony)**: Pleasing color combinations
- **[Color Picker](/glossary/color-picker)**: Tools for color selection
- **[Image Processing](/glossary/image-processing)**: Digital image manipulation
- **[Computer Vision](/glossary/computer-vision)**: Automated image analysis
- **[Color Conversion](/glossary/color-conversion)**: Converting between color spaces
## Tools and Resources
### RGBmatic Tools
- **[HSV Color Picker](/tools/hsv-color-picker)**: Interactive HSV color selection
- **[RGB to HSV Converter](/tools/rgb-to-hsv)**: Convert between RGB and HSV
- **[Color Harmony Generator](/tools/color-harmony)**: Create HSV-based color schemes
- **[Image Color Analyzer](/tools/image-color-analyzer)**: Analyze HSV distribution in images
### External Resources
- [HSV Color Model - Wikipedia](https://en.wikipedia.org/wiki/HSL_and_HSV)
- [Color Theory for Designers](https://www.interaction-design.org/literature/topics/color-theory)
- [Computer Vision Color Spaces](https://docs.opencv.org/master/df/d9d/tutorial_py_colorspaces.html)
- [Digital Image Processing](https://www.imageprocessingplace.com/)
HSV provides an intuitive and powerful framework for color manipulation, making it essential for digital design, image processing, and computer vision applications where human-perceptual color relationships are important.