This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet as we cannot guarantee the safety of other websites.
For more information, see HSL and HSV on Wikipedia.

HSV is an alternative representation of the RGB color model using a cylindrical coordinate system. HSV stands for hue, saturation, and value. In the cylindrical coordinate system that wraps around an axis, these correspond to the angle around the axis, distance away from the axis, and distance along the axis, respectively.

Usage in Scratch

The color picker in the paint editor

HSV is found in Scratch in a few places. Scratch uses the term color to refer to hue and brightness to refer to value.

Formulas

The formulas shown are for 0H<360, 0S1, 0V1

HSV to RGB

C=VS

X=C(1|H60 mod21|)

m=VC

(R,G,B)={(C,X,0)0H<60(X,C,0)60H<120(0,C,X)120H<180(0,X,C)180H<240(X,0,C)240H<300(C,0,X)300H<360

(R,G,B)=(255(R+m),255(G+m),255(B+m)) [1]

The following Scratch code implements it with all values in the range [0-1) (easy to modify to suit a desired range such as 0-255):

define HSV to RGB (h) (s) (v)
set [hue v] to (((h) mod (1)) * (6)) // remap h to [0,6)
if <(hue) < [3]> then
  if <(hue) < [1]> then
    set [r v] to (v)
    set [g v] to ((v) * ((1) - ((s) * ((1) - ((hue) mod (1))))))
    set [b v] to ((v) * ((1) - (s)))
  else
    if <(hue) < [2]> then
      set [r v] to ((v) * ((1) - ((s) * ((hue) mod (1)))))
      set [g v] to (v)
      set [b v] to ((v) * ((1) - (s)))
    else
      set [r v] to ((v) * ((1) - (s)))
      set [g v] to (v)
      set [b v] to ((v) * ((1) - ((s) * ((1) - ((hue) mod (1))))))
    end
  end
else
  if <(hue) < [4]> then
    set [r v] to ((v) * ((1) - (s)))
    set [g v] to ((v) * ((1) - ((s) * ((hue) mod (1)))))
    set [b v] to (v)
  else
    if <(hue) < [5]> then
      set [r v] to ((v) * ((1) - ((s) * ((1) - ((hue) mod (1))))))
      set [g v] to ((v) * ((1) - (s)))
      set [b v] to (v)
    else
      set [r v] to (v)
      set [g v] to ((v) * ((1) - (s)))
      set [b v] to ((v) * ((1) - ((s) * ((hue) mod (1)))))
    end
  end
end


RGB to HSV

R=R255

G=G255

B=B255

Cmax=max(R,G,B)

Cmin=min(R,G,B)

Δ=CmaxCmin

H={0Δ=060(GBΔmod6)Cmax=R60(BRΔ+2)Cmax=G60(RGΔ+4)Cmax=B

S={0Cmax=0ΔCmaxCmax=0

V=Cmax [2]

The following Scratch code implements it with all values in the range [0-1) (easy to modify to suit a desired range such as 0-255):

define RGB to HSV (r) (g) (b)
if <(r) < (b)> then
  if <(r) < (g)> then
    if <(g) < (b)> then
      set [c min v] to (r)
      set [c max v] to (b)
    else
      set [c min v] to (r)
      set [c max v] to (g)
    end
  else
    set [c min v] to (g)
    set [c max v] to (b)
  end
else
  if <(g) < (b)> then
    set [c min v] to (g)
    set [c max v] to (r)
  else
    if <(r) < (g)> then
      set [c min v] to (b)
      set [c max v] to (g)
    else
      set [c min v] to (b)
      set [c max v] to (r)
    end
  end
end
set [diff v] to ((c max)-(c min))
if <(diff) = [0]> then // greyscale
  set [h v] to [0]
  set [s v] to [0]
else
  if <(r) = (c max)> then
    set [h v] to (((((g) - (b)) / (diff)) / (6)) mod (1))
  else
    if <(g) = (v)> then
      set [h v] to ((((2) + (((b) - (r)) / (diff))) / (6)) mod (1))
    else
      set [h v] to ((((4) + (((r) - (g)) / (diff))) / (6)) mod (1))
    end
  end
  set [s v] to ((diff)/(c max))
end
set [v v] to (c max)


Example Projects

  • HSV/RGB converter by awesome-llama-test — HSV to RGB and RGB to HSV, using ranges of 0.0-1.0. Outputs to 3 variables.
  • RGB⇔HSV by Syorirewan — HSV to RGB and RGB to HSV, using ranges of 0-255. Outputs to 3 variables.
  • rgb2hsv by Spentinium — RGB to HSV. Outputs to 3 variables.
  • hsv2rgb by Spentinium — HSV to RGB. Outputs a number 0-16777215.

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.