Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. local function HSVtoRGB(h, s, v)
  2.  
  3. local r,g,b = 0,0,0
  4. local tempS = s / 100
  5. local tempV = v / 100
  6.  
  7. local calc = h / 60
  8. local calc_floor = math.floor(calc)
  9.  
  10. local hi = calc_floor % 6
  11. local f = calc - calc_floor
  12. local p = (tempV * (1 - tempS))
  13.  
  14. if(hi==0) then
  15. r = tempV
  16. g = (tempV * (1 - (1 - f) * tempS))
  17. b = p
  18. elseif(hi==1) then
  19. r = (tempV * (1 - f * tempS))
  20. g = tempV
  21. b = p
  22. elseif(hi==2) then
  23. r = p
  24. g = tempV
  25. b = (tempV * (1 - (1 - f) * tempS))
  26. elseif(hi==3) then
  27. r = p
  28. g = (tempV * (1 - f * tempS))
  29. b = tempV
  30. elseif(hi==4) then
  31. r = (tempV * (1 - (1 - f) * tempS))
  32. g = p
  33. b = tempV
  34. elseif(hi==5) then
  35. r = tempV
  36. g = p
  37. b = (tempV * (1 - f * tempS))
  38. end
  39.  
  40. return r * 255, g * 255, b * 255
  41.  
  42. end
  43.  
  44. local function HUEtoRGB( h )
  45.  
  46. local r,g,b = 0,0,0
  47.  
  48. local z = math.floor(h / 60)
  49. local hi = z % 6
  50. local f = h / 60 - z
  51.  
  52. if(hi==0) then
  53. r = 1
  54. g = f
  55. b = 0
  56. elseif(hi==1) then
  57. r = (1 - f)
  58. g = 1
  59. b = 0
  60. elseif(hi==2) then
  61. r = 0
  62. g = 1
  63. b = f
  64. elseif(hi==3) then
  65. r = 0
  66. g = (1 - f)
  67. b = 1
  68. elseif(hi==4) then
  69. r = f
  70. g = 0
  71. b = 1
  72. elseif(hi==5) then
  73. r = 1
  74. g = 0
  75. b = (1 - f)
  76. end
  77.  
  78. return r * 255, g * 255, b * 255
  79.  
  80. end
  81.  
  82. local function RGBToHSV( red, green, blue )
  83.  
  84. local hue, saturation, value
  85.  
  86. local min_value = math.min( red, green, blue )
  87. local max_value = math.max( red, green, blue )
  88.  
  89. value = max_value
  90.  
  91. local value_delta = max_value - min_value
  92.  
  93. if max_value ~= 0 then
  94. saturation = value_delta / max_value
  95.  
  96. else
  97. saturation = 0
  98. hue = -1
  99. return hue, saturation, value
  100. end
  101.  
  102. if red == max_value then
  103. hue = ( green - blue ) / value_delta
  104. elseif green == max_value then
  105. hue = 2 + ( blue - red ) / value_delta
  106. else
  107. hue = 4 + ( red - green ) / value_delta
  108. end
  109.  
  110. hue = hue * 60
  111. if hue < 0 then
  112. hue = hue + 360
  113. end
  114.  
  115. if(hue ~= hue) then
  116. hue = 0
  117. end
  118.  
  119. return hue, saturation*100, value/2.55
  120. end
  121.  
  122. local function in_boundary(x1,y1,x2,y2)
  123. local mouse_x, mouse_y = input.GetMousePos()
  124.  
  125. if(mouse_x >= x1 and mouse_y >= y1 and mouse_x <= x1+x2 and mouse_y <= y1+y2) then
  126. return true
  127. end
  128.  
  129. return false
  130. end
  131.  
  132. local image = file.Open( "transparent.png", "r" )
  133. local data = image:Read()
  134. image:Close()
  135.  
  136. local img_rgba, img_width, img_height = common.DecodePNG( data )
  137. local texture = draw.CreateTexture( img_rgba, img_width, img_height )
  138.  
  139. local picker_x, picker_y, picker_width, picker_height = 400, 700, 200, 200
  140. local hue_x, hue_y, hue_width = ( picker_x + picker_width ) + 20, picker_y, 20
  141. local preview_x, preview_y, preview_width, preview_height = picker_x, ( picker_y + picker_height ) + 20, picker_x + picker_width, ( picker_y + picker_height + 20 ) + 40
  142.  
  143. local lmouse_hue_x, lmouse_hue_y = 0,0
  144. local lmouse_picker_x, lmouse_picker_y = 0,0
  145. local mouse_down_hue, mouse_down_picker = false, false
  146.  
  147. local hue, saturation, value = 50, 20, 70
  148. local r, g, b = HSVtoRGB( hue, saturation, value )
  149.  
  150. local function color_picker()
  151.  
  152. local screen_width, screen_height = draw.GetScreenSize()
  153. local mouse_x, mouse_y = input.GetMousePos()
  154.  
  155. draw.SetTexture(0, 0, 0, 0)
  156. draw.Color( HUEtoRGB( hue ) )
  157. draw.FilledRect( picker_x, picker_y, picker_x + picker_width, picker_y + picker_height )
  158.  
  159. draw.Color(255, 255, 255, 255)
  160. draw.SetTexture( texture )
  161. draw.FilledRect( picker_x, picker_y, picker_x + picker_width, picker_y + picker_height )
  162.  
  163. draw.SetTexture(0, 0, 0, 0)
  164. for i=0, 360, 1 do
  165.  
  166. draw.Color( HUEtoRGB( i * 360 / picker_height ) )
  167. draw.FilledRect( hue_x, hue_y + i, hue_x + hue_width, hue_y + picker_height )
  168.  
  169. end
  170.  
  171. if( input.IsButtonDown( "mouse1" ) and in_boundary( picker_x, picker_y, picker_width, picker_height ) ) then
  172. saturation = ( mouse_x - picker_x ) / picker_width * 100
  173.  
  174. value = ( (picker_y - mouse_y) / picker_height * 100 ) + 100
  175.  
  176. r,g,b = HSVtoRGB( hue, saturation, value )
  177.  
  178. lmouse_hue_x = mouse_x
  179. lmouse_hue_y = mouse_y
  180.  
  181. mouse_down_hue = true
  182. end
  183.  
  184. if( input.IsButtonDown( "mouse1" ) and in_boundary( hue_x, picker_y, hue_width, picker_height ) ) then
  185.  
  186. hue = math.floor( ( ( mouse_y - picker_y ) / picker_height ) * 360 )
  187. r,g,b = HSVtoRGB( hue, saturation, value )
  188.  
  189. lmouse_picker_x = mouse_x
  190. lmouse_picker_y = mouse_y
  191.  
  192. mouse_down_picker = true
  193. end
  194.  
  195. draw.Color( 54, 0, 0, 255 )
  196. if( mouse_down_hue ) then
  197. draw.OutlinedRect( lmouse_hue_x - 2, lmouse_hue_y - 2, lmouse_hue_x + 2, lmouse_hue_y + 2 )
  198. else
  199. draw.OutlinedRect( picker_x + ( saturation * ( picker_width / 100 ) ) - 2, picker_y - ( value * ( picker_height / 100 ) ) + picker_height - 2, picker_x + (saturation * ( picker_width / 100 ) ) + 2, ( picker_y-value * ( picker_height / 100 ) ) + picker_height + 2 )
  200. end
  201.  
  202. if( mouse_down_picker ) then
  203. draw.OutlinedRect( hue_x - 2, lmouse_picker_y - 2, hue_x + hue_width + 2, lmouse_picker_y + 2 )
  204. else
  205. draw.OutlinedRect( hue_x - 2, picker_y + ( ( picker_height / 360 ) * hue) - 2, hue_x + hue_width + 2, picker_y + ( ( picker_height / 360 ) * hue) + 2 )
  206. end
  207.  
  208. draw.Color( r, g , b , 255 )
  209. draw.FilledRect( preview_x, preview_y, preview_width, preview_height )
  210.  
  211. end
  212.  
  213.  
  214. callbacks.Register("Draw", "color_picker", color_picker)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement