Skip to content

Instantly share code, notes, and snippets.

@SkyTheCoder
Created August 7, 2013 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SkyTheCoder/ed1c139d64268c2c2f7b to your computer and use it in GitHub Desktop.
Save SkyTheCoder/ed1c139d64268c2c2f7b to your computer and use it in GitHub Desktop.
Codea Project Gist Created with AutoGist
AutoGist Tab Order Version: 2.2.8
------------------------------
This file should not be included in the Codea project.
#ColorChooser
#Main
ColorChooser = class()
function ColorChooser:init()
self.DIST = 90
local size = 361
local offset = 182
self.circle = image(size, size)
for hue = 1, 360 do
for len = 0, 0.06, 0.01 do
for dist = 0, self.DIST do
self.circle:set((math.sin((hue / 180 * math.pi) + len) * (180 - dist)) + offset, (math.cos((hue / 180 * math.pi) + len) * (180 - dist)) + offset, color(self:HSVtoRGB(hue / 360, 1, 1)))
end
end
end
self.triangle = mesh()
local len = 179 - self.DIST
self.triangle.vertices = {
vec2(math.sin((2 * math.pi) / 3) * len, math.cos((2 * math.pi) / 3) * len),
vec2(math.sin(2 * ((2 * math.pi) / 3)) * len, math.cos(2 * ((2 * math.pi) / 3)) * len),
vec2(math.sin(2 * math.pi) * len, math.cos(2 * math.pi) * len),
}
self.triangle.colors = {
color(0, 0, 0),
color(255, 255, 255),
color(255, 0, 0),
}
self.alpha = mesh()
self.w, self.h = 50, 360
self.x, self.y = 7 * (WIDTH / 8), HEIGHT / 2
self.alpha.vertices = {
vec2(-(self.w / 2), -(self.h / 2)), vec2(-(self.w / 2), self.h / 2), vec2(self.w / 2, -(self.h / 2)),
vec2(self.w / 2, self.h / 2), vec2(self.w / 2, -(self.h / 2)), vec2(-(self.w / 2), self.h / 2),
}
self.alpha.colors = {
color(0, 0, 0, 0), color(255, 0, 0, 255), color(0, 0, 0, 0),
color(255, 0, 0, 255), color(0, 0, 0, 0), color(255, 0, 0, 255),
}
self.ANGLE = 0
self.ON_RING = false
self.RING_ID = 0
self.ON_TRIANGLE = false
self.TRIANGLE_ID = 0
self.ON_ALPHA = false
self.ALPHA_ID = 0
self.screen = image(WIDTH, HEIGHT)
self.alphaScreen = image(self.w, self.h)
self.pickedColor = color(255, 0, 0, 255)
end
function ColorChooser:draw()
pushStyle()
pushMatrix()
noStroke()
self.triangle.colors = {
color(0, 0, 0),
color(255, 255, 255),
color(self.circle:get((math.sin(-self.ANGLE) * 170) + 181, (math.cos(-self.ANGLE) * 170) + 181)),
}
local r, g, b = self.pickedColor.r, self.pickedColor.g, self.pickedColor.b
local col = color(r, g, b, 255)
self.alpha.colors = {
color(0, 0, 0, 0), col, color(0, 0, 0, 0),
col, color(0, 0, 0, 0), col,
}
translate(WIDTH / 2, HEIGHT / 2)
rotate(math.deg(self.ANGLE))
self.triangle:draw()
resetMatrix()
setContext(self.screen)
background(0, 0, 0, 0)
translate(WIDTH / 2, HEIGHT / 2)
rotate(math.deg(self.ANGLE))
self.triangle:draw()
setContext()
resetMatrix()
sprite(self.circle, WIDTH / 2, HEIGHT / 2)
translate(self.x, self.y)
self.alpha:draw()
resetMatrix()
setContext(self.alphaScreen)
background(255, 255, 255, 0)
translate(self.w / 2, self.h / 2)
self.alpha:draw()
resetMatrix()
setContext()
popStyle()
popMatrix()
end
function ColorChooser:touched(touch)
local tx, ty = touch.x, touch.y
local xc, yc = WIDTH / 2, HEIGHT / 2
local a, b, c, d = 90, 90, 180, 180
if touch.state == BEGAN and touch.id ~= self.RING_ID and touch.id ~= self.ALPHA_ID and not self.ON_TRIANGLE and (ty-yc)^2/b^2+(tx-xc)^2/a^2 <= 1 then
self.ON_TRIANGLE = true
self.TRIANGLE_ID = touch.id
if color(self.screen:get(touch.x, touch.y)).a ~= 0 then
self.pickedColor = color(self.screen:get(touch.x, touch.y))
end
elseif touch.state == BEGAN and touch.id ~= self.TRIANGLE_ID and touch.id ~= self.ALPHA_ID and not self.ON_RING and (ty-yc)^2/d^2+(tx-xc)^2/c^2 <= 1 then
self.ON_RING = true
self.RING_ID = touch.id
self.ANGLE = math.atan2(touch.y - (HEIGHT / 2), touch.x - (WIDTH / 2)) - (math.pi / 2)
elseif touch.state == BEGAN and touch.id ~= self.RING_ID and touch.id ~= self.TRIANGLE_ID and not self.ON_ALPHA and touch.x >= self.x - (self.w / 2) and touch.y >= self.y - (self.h / 2) and touch.x <= self.x + (self.w / 2) and touch.y <= self.y + (self.h / 2) then
self.ON_ALPHA = true
self.ALPHA_ID = touch.id
local r, g, b, a = self.alphaScreen:get(math.max(1, math.min(self.w, touch.x - (self.x - (self.w / 2)))), math.max(1, math.min(self.h, touch.y - (self.y - (self.h / 2)))))
self.pickedColor.a = a
end
if touch.state == MOVING and self.ON_RING and self.RING_ID == touch.id then
self.ANGLE = math.atan2(touch.y - (HEIGHT / 2), touch.x - (WIDTH / 2)) - (math.pi / 2)
elseif touch.state == MOVING and self.ON_TRIANGLE and self.TRIANGLE_ID == touch.id then
if color(self.screen:get(touch.x, touch.y)).a ~= 0 then
self.pickedColor = color(self.screen:get(touch.x, touch.y))
end
elseif touch.state == MOVING and self.ON_ALPHA and self.ALPHA_ID == touch.id then
local r, g, b, a = self.alphaScreen:get(math.max(1, math.min(self.w, touch.x - (self.x - (self.w / 2)))), math.max(1, math.min(self.h, touch.y - (self.y - (self.h / 2)))))
self.pickedColor.a = a
end
if self.ON_RING and self.RING_ID == touch.id and (touch.state == ENDED or touch.state == CANCELLED) then
self.ON_RING = false
self.RING_ID = 0
elseif self.ON_TRIANGLE and self.TRIANGLE_ID == touch.id and (touch.state == ENDED or touch.state == CANCELLED) then
self.ON_TRIANGLE = false
self.TRIANGLE_ID = 0
elseif self.ON_ALPHA and self.ALPHA_ID == touch.id and (touch.state == ENDED or touch.state == CANCELLED) then
self.ON_ALPHA = false
self.ALPHA_ID = 0
end
end
-- From a guest on pastebin.com.
-- Slightly modified indentations to look prettier.
-- URL: http://pastebin.com/NrcJgL1d
-----------------------------
-- HSV > RGB color conversion
-----------------------------
-- adapted from:
-- http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
-----------------------------
function ColorChooser:HSVtoRGB(h, s, v)
local r, g, b
local i = math.floor(h * 6)
local f = h * 6 - i
local p = v * (1 - s)
local q = v * (1 - f * s)
local t = v * (1 - (1 - f) * s)
local switch = i % 6
if switch == 0 then
r = v
g = t
b = p
elseif switch == 1 then
r = q
g = v
b = p
elseif switch == 2 then
r = p
g = v
b = t
elseif switch == 3 then
r = p
g = q
b = v
elseif switch == 4 then
r = t
g = p
b = v
elseif switch == 5 then
r = v
g = p
b = q
end
return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
end
-- Color Chooser
function setup()
cc = ColorChooser()
end
function draw()
background(40, 40, 50)
cc:draw()
fill(cc.pickedColor)
rect(0, 0, 100, 100)
end
function touched(touch)
cc:touched(touch)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment