Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function HSVtoRGB(h, s, v)
- local r,g,b = 0,0,0
- local tempS = s / 100
- local tempV = v / 100
- local calc = h / 60
- local calc_floor = math.floor(calc)
- local hi = calc_floor % 6
- local f = calc - calc_floor
- local p = (tempV * (1 - tempS))
- if(hi==0) then
- r = tempV
- g = (tempV * (1 - (1 - f) * tempS))
- b = p
- elseif(hi==1) then
- r = (tempV * (1 - f * tempS))
- g = tempV
- b = p
- elseif(hi==2) then
- r = p
- g = tempV
- b = (tempV * (1 - (1 - f) * tempS))
- elseif(hi==3) then
- r = p
- g = (tempV * (1 - f * tempS))
- b = tempV
- elseif(hi==4) then
- r = (tempV * (1 - (1 - f) * tempS))
- g = p
- b = tempV
- elseif(hi==5) then
- r = tempV
- g = p
- b = (tempV * (1 - f * tempS))
- end
- return r * 255, g * 255, b * 255
- end
- local function HUEtoRGB( h )
- local r,g,b = 0,0,0
- local z = math.floor(h / 60)
- local hi = z % 6
- local f = h / 60 - z
- if(hi==0) then
- r = 1
- g = f
- b = 0
- elseif(hi==1) then
- r = (1 - f)
- g = 1
- b = 0
- elseif(hi==2) then
- r = 0
- g = 1
- b = f
- elseif(hi==3) then
- r = 0
- g = (1 - f)
- b = 1
- elseif(hi==4) then
- r = f
- g = 0
- b = 1
- elseif(hi==5) then
- r = 1
- g = 0
- b = (1 - f)
- end
- return r * 255, g * 255, b * 255
- end
- local function RGBToHSV( red, green, blue )
- local hue, saturation, value
- local min_value = math.min( red, green, blue )
- local max_value = math.max( red, green, blue )
- value = max_value
- local value_delta = max_value - min_value
- if max_value ~= 0 then
- saturation = value_delta / max_value
- else
- saturation = 0
- hue = -1
- return hue, saturation, value
- end
- if red == max_value then
- hue = ( green - blue ) / value_delta
- elseif green == max_value then
- hue = 2 + ( blue - red ) / value_delta
- else
- hue = 4 + ( red - green ) / value_delta
- end
- hue = hue * 60
- if hue < 0 then
- hue = hue + 360
- end
- if(hue ~= hue) then
- hue = 0
- end
- return hue, saturation*100, value/2.55
- end
- local function in_boundary(x1,y1,x2,y2)
- local mouse_x, mouse_y = input.GetMousePos()
- if(mouse_x >= x1 and mouse_y >= y1 and mouse_x <= x1+x2 and mouse_y <= y1+y2) then
- return true
- end
- return false
- end
- local image = file.Open( "transparent.png", "r" )
- local data = image:Read()
- image:Close()
- local img_rgba, img_width, img_height = common.DecodePNG( data )
- local texture = draw.CreateTexture( img_rgba, img_width, img_height )
- local picker_x, picker_y, picker_width, picker_height = 400, 700, 200, 200
- local hue_x, hue_y, hue_width = ( picker_x + picker_width ) + 20, picker_y, 20
- 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
- local lmouse_hue_x, lmouse_hue_y = 0,0
- local lmouse_picker_x, lmouse_picker_y = 0,0
- local mouse_down_hue, mouse_down_picker = false, false
- local hue, saturation, value = 50, 20, 70
- local r, g, b = HSVtoRGB( hue, saturation, value )
- local function color_picker()
- local screen_width, screen_height = draw.GetScreenSize()
- local mouse_x, mouse_y = input.GetMousePos()
- draw.SetTexture(0, 0, 0, 0)
- draw.Color( HUEtoRGB( hue ) )
- draw.FilledRect( picker_x, picker_y, picker_x + picker_width, picker_y + picker_height )
- draw.Color(255, 255, 255, 255)
- draw.SetTexture( texture )
- draw.FilledRect( picker_x, picker_y, picker_x + picker_width, picker_y + picker_height )
- draw.SetTexture(0, 0, 0, 0)
- for i=0, 360, 1 do
- draw.Color( HUEtoRGB( i * 360 / picker_height ) )
- draw.FilledRect( hue_x, hue_y + i, hue_x + hue_width, hue_y + picker_height )
- end
- if( input.IsButtonDown( "mouse1" ) and in_boundary( picker_x, picker_y, picker_width, picker_height ) ) then
- saturation = ( mouse_x - picker_x ) / picker_width * 100
- value = ( (picker_y - mouse_y) / picker_height * 100 ) + 100
- r,g,b = HSVtoRGB( hue, saturation, value )
- lmouse_hue_x = mouse_x
- lmouse_hue_y = mouse_y
- mouse_down_hue = true
- end
- if( input.IsButtonDown( "mouse1" ) and in_boundary( hue_x, picker_y, hue_width, picker_height ) ) then
- hue = math.floor( ( ( mouse_y - picker_y ) / picker_height ) * 360 )
- r,g,b = HSVtoRGB( hue, saturation, value )
- lmouse_picker_x = mouse_x
- lmouse_picker_y = mouse_y
- mouse_down_picker = true
- end
- draw.Color( 54, 0, 0, 255 )
- if( mouse_down_hue ) then
- draw.OutlinedRect( lmouse_hue_x - 2, lmouse_hue_y - 2, lmouse_hue_x + 2, lmouse_hue_y + 2 )
- else
- 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 )
- end
- if( mouse_down_picker ) then
- draw.OutlinedRect( hue_x - 2, lmouse_picker_y - 2, hue_x + hue_width + 2, lmouse_picker_y + 2 )
- else
- draw.OutlinedRect( hue_x - 2, picker_y + ( ( picker_height / 360 ) * hue) - 2, hue_x + hue_width + 2, picker_y + ( ( picker_height / 360 ) * hue) + 2 )
- end
- draw.Color( r, g , b , 255 )
- draw.FilledRect( preview_x, preview_y, preview_width, preview_height )
- end
- callbacks.Register("Draw", "color_picker", color_picker)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement