New topic: Anyone with experience creating color wheel?
<http://forums.realsoftware.com/viewtopic.php?t=37063> Page 1 of 1 [ 6 posts ] Previous topic | Next topic Author Message ChickenScratch Post subject: Anyone with experience creating color wheel?Posted: Thu Jan 13, 2011 11:47 am Joined: Fri Feb 15, 2008 5:14 pm Posts: 111 I'm trying to learn coloring on canvas and tried to create some kind of a color wheel but I'm failing miserably. I originally thought it would be rather simple. 1) select a center point (x, y) 2) define a radius 3) get the points along the edges (I think this how you get the points): Code: ' i = angle x1 = x + cos(i) * radius y1 = y + sin(i) * radius 4) draw lines from center to end points * 360 Has anyone done this before and can point me in the right direction? Thanks Top trs Post subject: Re: Anyone with experience creating color wheel?Posted: Thu Jan 13, 2011 12:48 pm Joined: Thu May 03, 2007 2:33 pm Posts: 164 something like this: Code:dim px,py as double dim oldpx,oldpy as double for i as integer = 0 to 360 g.forecolor=hsv(i/360,1,1) px=x+cos(i*3.141592654/180)*radius py=y+sin(i*3.141592654/180)*radius if i<>0 then drawtriangle g,x,y,px,py,oldpx,oldpy oldpx=px oldpy=py next Then put in some method to implement 'drawtriangle' - this could wrap 'g.fillpolygon' to draw a filled triangle. Top ChickenScratch Post subject: Re: Anyone with experience creating color wheel?Posted: Thu Jan 13, 2011 1:57 pm Joined: Fri Feb 15, 2008 5:14 pm Posts: 111 trs wrote:something like this: Code:dim px,py as double dim oldpx,oldpy as double for i as integer = 0 to 360 g.forecolor=hsv(i/360,1,1) px=x+cos(i*3.141592654/180)*radius py=y+sin(i*3.141592654/180)*radius if i<>0 then drawtriangle g,x,y,px,py,oldpx,oldpy oldpx=px oldpy=py next Then put in some method to implement 'drawtriangle' - this could wrap 'g.fillpolygon' to draw a filled triangle. Thank you! This at least has gotten me off on a nice start. I think I'm having problems with the polygon as it only accepts integers, so I ended up rounding off the results of px, py via "round". This is probably where my wheel looks weird. Also, the colors don't quite blend in right. Here's a screenshot of the result. Again, I'm just learning so this is a great start. Thanks. Top ChickenScratch Post subject: Re: Anyone with experience creating color wheel?Posted: Thu Jan 13, 2011 2:22 pm Joined: Fri Feb 15, 2008 5:14 pm Posts: 111 Okay, I think I have it - with some tricks. Here is a screenshot: I was having problems with triangles as note above, so I switched to ovals with 2pix height and 2 pix width. I also made slight changes to the code: Code: for i as integer = 0 to 360 angleR = i * (pi/180) if i<>0 then g.forecolor = hsv(i/360, 1.0, 1.0) for k as integer = 0 to radius px = round( x - cos(angleR) * (radius - k) ) py = round( y - sin(angleR) * (radius - k) ) g.filloval(px, py, 2, 2) next end if next Somehow, this still doesn't feel quite right to me for some reason. I feel like it was a hack job or some sort (my version, that is). I had to cover up the middle because the center point was not aligned properly. Top doofus Post subject: Re: Anyone with experience creating color wheel?Posted: Thu Jan 13, 2011 6:30 pm Joined: Thu Sep 10, 2009 2:50 am Posts: 61 Location: Santa Cruz, CA, USA Tracing out the circle will leave pixel gaps unless you use lots of points or large dots. Instead scan the pixels then measure the angle and radius to determine the color. Code: dim pic As Picture = NewPicture(300, 300, 32) dim surf As RGBSurface = pic.RGBSurface dim x, y, centerX, centerY, xdiff, ydiff As integer dim radius, rawAngle, hueAngle, maxRadius As double centerX = 150 centerY = 150 maxRadius = 140 for y = 0 to 299 ydiff = y - centerY for x = 0 to 299 xdiff = x - centerX radius = Sqrt(xdiff * xdiff + ydiff * ydiff) if radius <= maxRadius then 'test a minRadius for hole rawAngle = ATan2(ydiff, xdiff) hueAngle = rawAngle / 6.283185 + 0.5 //surf.Pixel(x, y) = HSV(hueAngle, 1.0, 1.0) //surf.Pixel(x, y) = HSV(hueAngle, 1.0, radius / maxRadius) surf.Pixel(x, y) = HSV(hueAngle, radius / maxRadius, 1.0) end next next //save pic somewhere and redraw ATan2 gives you the angle in range [-pi ... pi], which is then scaled to [0...1]. Change xdiff to "centerX - x" to have red on the right side. Top ChickenScratch Post subject: Re: Anyone with experience creating color wheel?Posted: Thu Jan 13, 2011 7:39 pm Joined: Fri Feb 15, 2008 5:14 pm Posts: 111 doofus wrote:ATan2 gives you the angle in range [-pi ... pi], which is then scaled to [0...1]. Change xdiff to "centerX - x" to have red on the right side. Very neat indeed. So much to learn, thank you so very much (both of you). This method is much, much cleaner. I never realized color is such a big topic... ahaha. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 6 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
