Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Colorpicker
- Colorpicker = class()
- function Colorpicker:init(x, y, r, b)
- self.x = x
- self.y = y
- self.r = r
- self.b = b
- local x1 = math.sqrt(r^2 - (r/2)^2)
- local y1 = math.sqrt(r^2 - x1^2)
- local x2 = math.sqrt((r-b)^2 - ((r-b)/2)^2)
- local y2 = math.sqrt((r-b)^2 - x2^2)
- self.m = mesh()
- local tbl = {
- -- hexagon
- vec2(0, r), vec2(0, r - b), vec2(x1, y1), vec2(x2, y2),
- vec2(x1, -y1), vec2(x2, -y2), vec2(0, -r), vec2(0, -(r-b)),
- vec2(-x1, -y1), vec2(-x2, -y2), vec2(-x1, y1), vec2(-x2, y2),
- }
- --calc side of smallest hexagon, used to calculate the square
- local sq = math.sqrt((tbl[2].x + tbl[4].x)^2 + (tbl[4].y - tbl[2].y)^2)
- sq = (3-math.sqrt(3))*sq
- sq = sq/2
- self.sq = sq
- tbl[13] = vec2(-sq, sq)
- tbl[14] = vec2(-sq, -sq)
- tbl[15] = vec2(sq, -sq)
- tbl[16] = vec2(sq, sq)
- local col ={
- -- hexagon
- color(255, 0, 0, 255), color(255, 255, 0, 255), color(0, 255, 0, 255),
- color(0, 255, 255, 255), color(0, 0, 255, 255), color(255, 0, 255, 255),
- -- rect
- color(127, 127, 127, 255), color(0), color(255), color(255, 0, 0, 255)
- }
- local v= {}
- local c= {}
- -----
- for i = 0, 5 do
- local a = 1+i*2
- local b = a+ 2
- if b == 13 then
- b = 1
- end
- local d = i+2
- if d == 7 then
- d = 1
- end
- v[1+i*6] = tbl[a] c[1+i*6] = col[i+1]
- v[2+i*6] = tbl[a+1] c[2+i*6] = col[i+1]
- v[3+i*6] = tbl[b] c[3+i*6] = col[d]
- v[4+i*6] = tbl[b] c[4+i*6] = col[d]
- v[5+i*6] = tbl[b+1] c[5+i*6] = col[d]
- v[6+i*6] = tbl[a+1] c[6+i*6] = col[i+1]
- end
- self.square = mesh()
- vec = {}
- cc = {}
- vec[1] = tbl[13] cc[1] = col[9]
- vec[2] = tbl[14] cc[2] = col[7]
- vec[3] = tbl[15] cc[3] = col[8]
- vec[4] = tbl[15] cc[4] = col[8]
- vec[5] = tbl[16] cc[5] = col[10]
- vec[6] = tbl[13] cc[6] = col[9]
- self.m.vertices = v
- self.m.colors = c
- self.square.vertices = vec
- self.square.colors = cc
- self.screen = image(WIDTH, HEIGHT)
- self.pickedcolor = color(0)
- self.ring = vec2(0, self.r - self.b/2)
- self.squar= vec2(sq, sq)
- end
- function Colorpicker:draw()
- self.pickedcolor = color(self.screen:get(self.x + self.squar.x, self.y + self.squar.y))
- pushMatrix()
- pushStyle()
- setContext(self.screen)
- background(self.pickedcolor)
- translate(self.x, self.y)
- self.m:draw()
- self.square:draw()
- setContext()
- sprite(self.screen, 0, 0)
- stroke(255, 255, 255, 255)
- fill(0, 0, 0, 0)
- strokeWidth(2)
- ellipse(self.ring.x, self.ring.y, self.b)
- ellipse(self.squar.x, self.squar.y, 25)
- popMatrix()
- popStyle()
- end
- function Colorpicker:InRect(t, r)
- ybound = r/2
- y = math.abs(t.y)
- xbound = math.sqrt(r^2 - (r/2)^2)
- x = math.abs(t.x)
- if y <= ybound and x <= xbound then
- return true
- end
- return false
- end
- function Colorpicker:InTriangle(t, r)
- ybound = r/2
- xbound = math.sqrt(r^2 - ybound^2)
- x = math.abs(t.x)
- y = math.abs(t.y)
- a = math.atan((y - ybound)/(xbound - x))
- if math.deg(a) > 0 and math.deg(a) < 30 then
- return true
- end
- return false
- end
- function Colorpicker:touched(t)
- touch = vec2(t.x - self.x, t.y - self.y)
- xbound = math.sqrt(self.r^2 - (self.r/2)^2)
- x = math.abs(touch.x)
- y = math.abs(touch.y)
- if x < xbound then
- if (self:InRect(touch, self.r) or self:InTriangle(touch, self.r)) and (not self:InTriangle(touch, self.r - self.b) and not self:InRect(touch, self.r - self.b)) then
- self.square.colors = {color(255), color(127, 127, 127, 255), color(0), color(0),
- color(self.screen:get(t.x, t.y)), color(255)}
- self.ring = touch
- end
- if x <= math.abs(self.sq) and y <= math.abs(self.sq) then
- self.squar = touch
- end
- end
- end
- --# Main
- --The name of the project must match your Codea project name if dependencies are used.
- --Project: Color Picker
- --Version: Alpha 1.0
- --Comments: Very first version, still some bugs...
- function setup()
- lol = Colorpicker(WIDTH/2, HEIGHT/2, 200, 50)
- end
- function draw()
- background(255, 255, 255, 255)
- lol:draw()
- end
- function touched(t)
- lol:touched(t)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement