Advertisement
Guest User

color

a guest
Nov 14th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.68 KB | None | 0 0
  1.  
  2. --# Colorpicker
  3. Colorpicker = class()
  4.  
  5. function Colorpicker:init(x, y, r, b)
  6.     self.x = x
  7.     self.y = y
  8.     self.r = r
  9.     self.b = b
  10.    
  11.     local x1 = math.sqrt(r^2 - (r/2)^2)
  12.     local y1 = math.sqrt(r^2 - x1^2)
  13.    
  14.     local x2 = math.sqrt((r-b)^2 - ((r-b)/2)^2)
  15.     local y2 = math.sqrt((r-b)^2 - x2^2)
  16.    
  17.     self.m = mesh()
  18.    
  19.     local tbl = {
  20.         -- hexagon
  21.         vec2(0, r), vec2(0, r - b), vec2(x1, y1), vec2(x2, y2),
  22.         vec2(x1, -y1), vec2(x2, -y2), vec2(0, -r), vec2(0, -(r-b)),
  23.         vec2(-x1, -y1), vec2(-x2, -y2), vec2(-x1, y1), vec2(-x2, y2),
  24.     }
  25.    
  26.     --calc side of smallest hexagon, used to calculate the square
  27.     local sq = math.sqrt((tbl[2].x + tbl[4].x)^2 + (tbl[4].y - tbl[2].y)^2)
  28.     sq = (3-math.sqrt(3))*sq
  29.     sq = sq/2
  30.     self.sq = sq
  31.    
  32.     tbl[13] = vec2(-sq, sq)
  33.     tbl[14] = vec2(-sq, -sq)
  34.     tbl[15] = vec2(sq, -sq)
  35.     tbl[16] = vec2(sq, sq)
  36.    
  37.     local col ={
  38.         -- hexagon
  39.         color(255, 0, 0, 255), color(255, 255, 0, 255), color(0, 255, 0, 255),
  40.         color(0, 255, 255, 255), color(0, 0, 255, 255), color(255, 0, 255, 255),
  41.         -- rect
  42.         color(127, 127, 127, 255), color(0), color(255), color(255, 0, 0, 255)
  43.     }
  44.    
  45.     local  v= {}
  46.     local  c= {}
  47.    
  48.     -----
  49.     for i = 0, 5 do
  50.        
  51.         local a = 1+i*2
  52.         local b = a+ 2
  53.        
  54.         if b == 13 then
  55.             b = 1
  56.         end
  57.        
  58.         local d = i+2
  59.        
  60.         if d == 7 then
  61.             d = 1
  62.         end
  63.        
  64.         v[1+i*6] = tbl[a]    c[1+i*6] = col[i+1]
  65.         v[2+i*6] = tbl[a+1]    c[2+i*6] = col[i+1]
  66.         v[3+i*6] = tbl[b]    c[3+i*6] = col[d]
  67.        
  68.         v[4+i*6] = tbl[b]    c[4+i*6] = col[d]
  69.         v[5+i*6] = tbl[b+1]    c[5+i*6] = col[d]
  70.         v[6+i*6] = tbl[a+1]    c[6+i*6] = col[i+1]
  71.  
  72.        
  73.     end
  74.    
  75.     self.square = mesh()
  76.     vec = {}
  77.     cc = {}
  78.    
  79.     vec[1] = tbl[13]    cc[1] = col[9]
  80.     vec[2] = tbl[14]    cc[2] = col[7]
  81.     vec[3] = tbl[15]    cc[3] = col[8]
  82.    
  83.     vec[4] = tbl[15]    cc[4] = col[8]
  84.     vec[5] = tbl[16]    cc[5] = col[10]
  85.     vec[6] = tbl[13]    cc[6] = col[9]
  86.    
  87.     self.m.vertices = v
  88.     self.m.colors = c
  89.     self.square.vertices = vec
  90.     self.square.colors = cc
  91.    
  92.     self.screen = image(WIDTH, HEIGHT)
  93.     self.pickedcolor = color(0)
  94.     self.ring = vec2(0, self.r - self.b/2)
  95.     self.squar= vec2(sq, sq)
  96. end
  97.  
  98. function Colorpicker:draw()
  99.    
  100.     self.pickedcolor = color(self.screen:get(self.x + self.squar.x, self.y + self.squar.y))
  101.    
  102.     pushMatrix()
  103.     pushStyle()
  104.     setContext(self.screen)
  105.     background(self.pickedcolor)
  106.     translate(self.x, self.y)
  107.     self.m:draw()
  108.     self.square:draw()
  109.     setContext()
  110.     sprite(self.screen, 0, 0)
  111.     stroke(255, 255, 255, 255)
  112.     fill(0, 0, 0, 0)
  113.     strokeWidth(2)
  114.     ellipse(self.ring.x, self.ring.y, self.b)
  115.     ellipse(self.squar.x, self.squar.y, 25)
  116.     popMatrix()
  117.     popStyle()
  118. end
  119.  
  120. function Colorpicker:InRect(t, r)
  121.    
  122.     ybound = r/2
  123.     y = math.abs(t.y)
  124.     xbound = math.sqrt(r^2 - (r/2)^2)
  125.     x = math.abs(t.x)
  126.    
  127.     if y <= ybound and x <= xbound then
  128.         return true
  129.     end
  130.     return false
  131.    
  132. end
  133.  
  134. function Colorpicker:InTriangle(t, r)
  135.    
  136.     ybound = r/2
  137.     xbound = math.sqrt(r^2 - ybound^2)
  138.     x = math.abs(t.x)
  139.     y = math.abs(t.y)
  140.    
  141.     a = math.atan((y - ybound)/(xbound - x))
  142.    
  143.     if math.deg(a) > 0 and math.deg(a) < 30 then
  144.         return true
  145.     end
  146.     return false
  147. end
  148.  
  149. function Colorpicker:touched(t)
  150.    
  151.     touch = vec2(t.x - self.x, t.y - self.y)
  152.    
  153.     xbound = math.sqrt(self.r^2 - (self.r/2)^2)
  154.     x = math.abs(touch.x)
  155.     y = math.abs(touch.y)
  156.    
  157.     if x < xbound then
  158.    
  159.     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
  160.        
  161.         self.square.colors = {color(255), color(127, 127, 127, 255), color(0), color(0),
  162.         color(self.screen:get(t.x, t.y)), color(255)}
  163.         self.ring = touch
  164.        
  165.     end
  166.    
  167.     if x <= math.abs(self.sq) and y <= math.abs(self.sq) then
  168.         self.squar = touch
  169.     end
  170.    
  171.     end
  172.    
  173. end
  174.  
  175. --# Main
  176. --The name of the project must match your Codea project name if dependencies are used.
  177. --Project: Color Picker
  178. --Version: Alpha 1.0
  179. --Comments: Very first version, still some bugs...
  180.  
  181. function setup()
  182.    
  183.     lol = Colorpicker(WIDTH/2, HEIGHT/2, 200, 50)
  184.    
  185. end
  186.  
  187. function draw()
  188.    
  189.     background(255, 255, 255, 255)
  190.    
  191.     lol:draw()
  192.    
  193. end
  194.  
  195.  
  196. function touched(t)
  197.    
  198.     lol:touched(t)
  199.    
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement