js 实现 RGB 转 HSV, RGB 转 YUV 色彩空间切换

公式参考

https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor

js 实现 RGB 转 HSV, RGB 转 YUV 色彩空间切换

代码参考

RGB -> HSV

function rgbToHsv([R, G, B, A]) {
  R /= 255
  G /= 255
  B /= 255
  const max = Math.max(R, G, B)
  const min = Math.min(R, G, B)
  const range = max - min
  let V = max
  let S = V === 0 ? 0 : range / V
  let H = 0
  if (R === V) H = (60 * (G - B)) / range
  if (G === V) H = 120 + (60 * (B - R)) / range
  if (B === V) H = 240 + (60 * (R - G)) / range
 
  if (range === 0) H = 0
  if (H < 0) H += 360
  H = (H / 2) * (256 / 180)
  S *= 255
  V *= 255
  return [H, S, V, A]
}

RGB -> YUV

function rgbToYuv([R, G, B, A]) {
  const Y = 0.299 * R + 0.587 * G + 0.114 * B
  const U = -0.147108 * R - 0.288804 * G + 0.435912 * B + 128
  const V = 0.614777 * R - 0.514799 * G - 0.099978 * B + 128
  return [Y, U, V, A]
}
上一篇:eas之action的创建


下一篇:cPage分页源码,分享给大家,可作参考