A comprehensive guide to YUV - an analog color space that separates luminance (Y) from chrominance (U, V) components, fundamental to television broadcasting, video encoding, and color image processing with efficient bandwidth utilization.
YUV is a color space used in analog television systems and video encoding that separates the luminance (brightness) component Y from the chrominance (color) components U and V. This separation takes advantage of human visual perception, which is more sensitive to brightness changes than color variations, enabling efficient transmission and storage of color video signals.
YUV represents color information through three distinct components:
// Comprehensive YUV color space processor
class YUVProcessor {
constructor() {
this.standards = {
'ntsc': {
name: 'NTSC (North America, Japan)',
weights: { wr: 0.299, wg: 0.587, wb: 0.114 },
uMax: 0.436,
vMax: 0.615
},
'pal': {
name: 'PAL (Europe, Asia)',
weights: { wr: 0.299, wg: 0.587, wb: 0.114 },
uMax: 0.436,
vMax: 0.615
},
'rec601': {
name: 'ITU-R Rec. 601',
weights: { wr: 0.299, wg: 0.587, wb: 0.114 },
uMax: 0.436,
vMax: 0.615
}
};
}
// Convert RGB to YUV using specified standard
rgbToYuv(r, g, b, standard = 'rec601') {
const config = this.standards[standard];
if (!config) {
throw new Error(`Unknown YUV standard: ${standard}`);
}
// Normalize RGB values to 0-1 range
const rNorm = r / 255;
const gNorm = g / 255;
const bNorm = b / 255;
// Calculate Y (luminance) using standard weights
const y = config.weights.wr * rNorm + config.weights.wg * gNorm + config.weights.wb * bNorm;
// Calculate U and V chrominance components
const u = config.uMax * (bNorm - y) / (1 - config.weights.wb);
const v = config.vMax * (rNorm - y) / (1 - config.weights.wr);
return {
y: Math.max(0, Math.min(1, y)),
u: Math.max(-config.uMax, Math.min(config.uMax, u)),
v: Math.max(-config.vMax, Math.min(config.vMax, v)),
standard,
// Convert to 8-bit representation
y8: Math.round(y * 255),
u8: Math.round((u + config.uMax) * 255 / (2 * config.uMax)),
v8: Math.round((v + config.vMax) * 255 / (2 * config.vMax))
};
}
// Convert YUV to RGB using specified standard
yuvToRgb(y, u, v, standard = 'rec601', inputFormat = 'normalized') {
const config = this.standards[standard];
if (!config) {
throw new Error(`Unknown YUV standard: ${standard}`);
}
let yNorm, uNorm, vNorm;
if (inputFormat === '8bit') {
// Convert from 8-bit to normalized values
yNorm = y / 255;
uNorm = (u * 2 * config.uMax / 255) - config.uMax;
vNorm = (v * 2 * config.vMax / 255) - config.vMax;
} else {
// Already normalized
yNorm = y;
uNorm = u;
vNorm = v;
}
// Convert to RGB
const r = yNorm + vNorm * (1 - config.weights.wr) / config.vMax;
const g = yNorm - uNorm * config.weights.wb * (1 - config.weights.wb) / (config.uMax * config.weights.wg) - vNorm * config.weights.wr * (1 - config.weights.wr) / (config.vMax * config.weights.wg);
const b = yNorm + uNorm * (1 - config.weights.wb) / config.uMax;
return {
r: Math.max(0, Math.min(255, Math.round(r * 255))),
g: Math.max(0, Math.min(255, Math.round(g * 255))),
b: Math.max(0, Math.min(255, Math.round(b * 255))),
standard
};
}
// Convert entire image from RGB to YUV
convertImageRgbToYuv(imageData, standard = 'rec601') {
const { data, width, height } = imageData;
const yChannel = new Float32Array(width * height);
const uChannel = new Float32Array(width * height);
const vChannel = new Float32Array(width * height);
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const pixelIndex = i / 4;
const yuv = this.rgbToYuv(r, g, b, standard);
yChannel[pixelIndex] = yuv.y;
uChannel[pixelIndex] = yuv.u;
vChannel[pixelIndex] = yuv.v;
}
return {
y: yChannel,
u: uChannel,
v: vChannel,
width,
height,
standard
};
}
// Convert YUV channels back to RGB image
convertYuvToImageRgb(yuvChannels) {
const { y, u, v, width, height, standard } = yuvChannels;
const result = new Uint8ClampedArray(width * height * 4);
for (let i = 0; i < y.length; i++) {
const rgb = this.yuvToRgb(y[i], u[i], v[i], standard, 'normalized');
const pixelIndex = i * 4;
result[pixelIndex] = rgb.r;
result[pixelIndex + 1] = rgb.g;
result[pixelIndex + 2] = rgb.b;
result[pixelIndex + 3] = 255; // Alpha
}
return new ImageData(result, width, height);
}
// Color correction for YUV images
adjustYuvColor(yuvChannels, adjustments = {}) {
const {
brightness = 0,
contrast = 1,
saturation = 1,
hueShift = 0
} = adjustments;
const { y, u, v, width, height, standard } = yuvChannels;
const adjustedY = new Float32Array(y.length);
const adjustedU = new Float32Array(u.length);
const adjustedV = new Float32Array(v.length);
for (let i = 0; i < y.length; i++) {
// Adjust brightness and contrast
adjustedY[i] = Math.max(0, Math.min(1, (y[i] + brightness) * contrast));
// Adjust saturation
const uSaturated = u[i] * saturation;
const vSaturated = v[i] * saturation;
// Apply hue shift
if (hueShift !== 0) {
const angle = hueShift * Math.PI / 180;
const cosAngle = Math.cos(angle);
const sinAngle = Math.sin(angle);
adjustedU[i] = uSaturated * cosAngle - vSaturated * sinAngle;
adjustedV[i] = uSaturated * sinAngle + vSaturated * cosAngle;
} else {
adjustedU[i] = uSaturated;
adjustedV[i] = vSaturated;
}
// Clamp chrominance values
const config = this.standards[standard];
adjustedU[i] = Math.max(-config.uMax, Math.min(config.uMax, adjustedU[i]));
adjustedV[i] = Math.max(-config.vMax, Math.min(config.vMax, adjustedV[i]));
}
return {
y: adjustedY,
u: adjustedU,
v: adjustedV,
width,
height,
standard,
adjustments
};
}
// Simulate analog TV transmission effects
simulateAnalogTransmission(yuvChannels, options = {}) {
const {
signalNoise = 0.02,
bandwidthLimitation = true,
chromaDelay = 0.5
} = options;
const { y, u, v, width, height, standard } = yuvChannels;
// Apply bandwidth limitation (blur)
const processedY = bandwidthLimitation ? this.applyBandwidthLimitation(y, width, height, 4.2) : y;
const processedU = bandwidthLimitation ? this.applyBandwidthLimitation(u, width, height, 1.3) : u;
const processedV = bandwidthLimitation ? this.applyBandwidthLimitation(v, width, height, 1.3) : v;
// Apply chroma delay
const delayedU = this.applyChromaDelay(processedU, width, height, chromaDelay);
const delayedV = this.applyChromaDelay(processedV, width, height, chromaDelay);
// Add noise
const noisyY = this.addNoise(processedY, signalNoise);
const noisyU = this.addNoise(delayedU, signalNoise * 0.7);
const noisyV = this.addNoise(delayedV, signalNoise * 0.7);
return {
y: noisyY,
u: noisyU,
v: noisyV,
width,
height,
standard,
transmissionEffects: { signalNoise, bandwidthLimitation, chromaDelay }
};
}
applyBandwidthLimitation(channel, width, height, bandwidthMHz) {
// Simulate bandwidth limitation with Gaussian blur
const result = new Float32Array(channel.length);
const sigma = Math.max(0.5, 5 - bandwidthMHz); // Higher bandwidth = less blur
const kernelSize = Math.ceil(sigma * 3) * 2 + 1;
// Apply horizontal blur (bandwidth limitation primarily affects horizontal resolution)
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let sum = 0;
let weightSum = 0;
for (let k = 0; k < kernelSize; k++) {
const kx = x + k - Math.floor(kernelSize / 2);
if (kx >= 0 && kx < width) {
const pixelIndex = y * width + kx;
const weight = Math.exp(-Math.pow(k - kernelSize/2, 2) / (2 * sigma * sigma));
sum += channel[pixelIndex] * weight;
weightSum += weight;
}
}
result[y * width + x] = weightSum > 0 ? sum / weightSum : channel[y * width + x];
}
}
return result;
}
applyChromaDelay(channel, width, height, delayPixels) {
const result = new Float32Array(channel.length);
const delay = Math.round(delayPixels);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const srcX = Math.max(0, x - delay);
const srcIndex = y * width + srcX;
const dstIndex = y * width + x;
result[dstIndex] = channel[srcIndex];
}
}
return result;
}
addNoise(channel, noiseLevel) {
const result = new Float32Array(channel.length);
for (let i = 0; i < channel.length; i++) {
const noise = (Math.random() - 0.5) * 2 * noiseLevel;
result[i] = Math.max(-1, Math.min(1, channel[i] + noise));
}
return result;
}
// Analyze YUV color characteristics
analyzeYuvCharacteristics(yuvChannels) {
const { y, u, v, standard } = yuvChannels;
const yStats = this.calculateChannelStatistics(y, 'Y (Luminance)');
const uStats = this.calculateChannelStatistics(u, 'U (Blue-Yellow)');
const vStats = this.calculateChannelStatistics(v, 'V (Red-Cyan)');
// Calculate overall saturation
let totalSaturation = 0;
for (let i = 0; i < u.length; i++) {
totalSaturation += Math.sqrt(u[i] * u[i] + v[i] * v[i]);
}
const averageSaturation = totalSaturation / u.length;
return {
luminance: yStats,
chrominanceU: uStats,
chrominanceV: vStats,
averageSaturation,
standard,
recommendations: this.generateYuvRecommendations(yStats, uStats, vStats, averageSaturation)
};
}
calculateChannelStatistics(channel, name) {
const values = Array.from(channel);
values.sort((a, b) => a - b);
const sum = values.reduce((acc, val) => acc + val, 0);
const mean = sum / values.length;
const variance = values.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / values.length;
const stdDev = Math.sqrt(variance);
return {
name,
min: Math.round(values[0] * 1000) / 1000,
max: Math.round(values[values.length - 1] * 1000) / 1000,
mean: Math.round(mean * 1000) / 1000,
median: Math.round(values[Math.floor(values.length / 2)] * 1000) / 1000,
standardDeviation: Math.round(stdDev * 1000) / 1000
};
}
generateYuvRecommendations(yStats, uStats, vStats, saturation) {
const recommendations = [];
if (yStats.standardDeviation < 0.1) {
recommendations.push('Low luminance contrast - consider brightness/contrast adjustment');
}
if (saturation < 0.05) {
recommendations.push('Low color saturation - image appears desaturated');
} else if (saturation > 0.3) {
recommendations.push('High color saturation - may appear oversaturated');
}
if (Math.abs(uStats.mean) > 0.1 || Math.abs(vStats.mean) > 0.1) {
recommendations.push('Color cast detected - consider white balance correction');
}
if (yStats.mean < 0.2) {
recommendations.push('Dark image - may benefit from brightness increase');
} else if (yStats.mean > 0.8) {
recommendations.push('Bright image - check for highlight clipping');
}
return recommendations;
}
}// YUV in television broadcasting systems
class BroadcastYUVProcessor extends YUVProcessor {
constructor() {
super();
this.broadcastStandards = {
'ntsc': { lines: 525, frameRate: 29.97, bandwidth: { y: 4.2, uv: 1.3 } },
'pal': { lines: 625, frameRate: 25, bandwidth: { y: 5.0, uv: 1.3 } },
'secam': { lines: 625, frameRate: 25, bandwidth: { y: 6.0, uv: 1.0 } }
};
}
// Prepare YUV for broadcast transmission
prepareBroadcastSignal(yuvChannels, standard = 'ntsc') {
const config = this.broadcastStandards[standard];
const { y, u, v, width, height } = yuvChannels;
// Apply pre-emphasis and filtering
const filteredY = this.applyBandwidthLimitation(y, width, height, config.bandwidth.y);
const filteredU = this.applyBandwidthLimitation(u, width, height, config.bandwidth.uv);
const filteredV = this.applyBandwidthLimitation(v, width, height, config.bandwidth.uv);
return {
y: filteredY,
u: filteredU,
v: filteredV,
width,
height,
standard,
broadcastReady: true
};
}
// Simulate broadcast reception effects
simulateReceptionEffects(broadcastSignal, receptionQuality = 'good') {
const effects = {
excellent: { noise: 0.005, ghosting: 0.02, colorShift: 0.01 },
good: { noise: 0.02, ghosting: 0.05, colorShift: 0.03 },
fair: { noise: 0.05, ghosting: 0.1, colorShift: 0.07 },
poor: { noise: 0.1, ghosting: 0.2, colorShift: 0.15 }
};
const effect = effects[receptionQuality] || effects.good;
return this.simulateAnalogTransmission(broadcastSignal, {
signalNoise: effect.noise,
bandwidthLimitation: true,
chromaDelay: effect.colorShift
});
}
}Explore these comprehensive pillar articles related to YUV
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Comprehensive guide to triadic color schemes - three colors evenly spaced around the color wheel that create balanced, vibrant, and dynamic visual compositions with mathematical precision and artistic appeal.
Discover more about YUV and related color theory concepts
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary