Advertisement
alihsaas

HSV to RGB

May 18th, 2020
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. fromHSV( Hue ∈ [0,360], Saturation ∈ [0,100], Value ∈ [0,100] )
  2. function fromHSV(H,S,V)
  3.     H = math.abs(H)
  4.  
  5.     local C = V * S
  6.  
  7.     local X = C * math.abs((H/60)%2-1)
  8.     local m = V - C
  9.  
  10.     local HSV
  11.     if H > 300 and H < 360 then
  12.         HSV = {C,0,X}
  13.     elseif H > 240 and H < 300 then
  14.         HSV = {X,0,C}
  15.     elseif H > 180 and H < 240 then
  16.         HSV = {0,X,C}
  17.     elseif H > 120 and H < 180 then
  18.         HSV = {0,C,X}
  19.     elseif H > 60 and H < 120 then
  20.         HSV = {X,C,0}
  21.     elseif H < 60 then
  22.         HSV = {C ,X, 0}
  23.     end
  24.  
  25.     local RGB = {(HSV[1]+m)*2.55,(HSV[2]+m)*2.55,(HSV[3]+m)*2.55}
  26. end
  27.  
  28.  
  29. --// method: convert Color3PlusData to Color3 userdata
  30. function Color3PlusData:toColor3()
  31.     return Color3.new(self.r,self.g,self.b);
  32. end
  33.  
  34. --// method: convert Color3PlusData to int
  35. function Color3PlusData:toInt()
  36.     return floor(self.r*255)*256^2+floor(self.g*255)*256+floor(self.b*255)
  37. end
  38. --// method: returns a tuple of hue, saturation and value data
  39. function toHSV()
  40.     local R,G,B = self.r*100,self.g*100,self.b*100
  41.     local R,G,B = 128,0,128
  42.     local Rp = R/255
  43.     local Gp = G/255
  44.     local Bp = B/255
  45.  
  46.     local Cmax = math.max(Rp, Gp, Bp)
  47.     local Cmin = math.min(Rp, Gp, Bp)
  48.    
  49.     local delta = Cmax - Cmin
  50.  
  51.     local H
  52.     if delta == 0 then
  53.          H = 0
  54.     elseif Cmax == Rp then
  55.         H = 60* (((Gp - Bp)/delta)%6 )
  56.     elseif Cmax == Gp then
  57.         H = 60* (((Bp - Rp)/delta) + 2 )
  58.     elseif Cmax == Bp then
  59.         H = 60* (((Rp - Gp)/delta) + 4 )
  60.     end
  61.  
  62.     local S
  63.     if Cmax == 0 then
  64.         S = 0
  65.     end
  66.     if Cmax ~= 0 then
  67.         S = delta/ Cmax *100
  68.     end
  69.  
  70.     local V = math.floor(Cmax*100)
  71.  
  72.     return H,S,V
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement