Last week I posted a tutorial showing how to enable finger painting in your Corona apps, and while I was putting together a sample app for that post I decided I wanted to enable users to change the color of their virtual paint from right within the app. So I did some searching on the web for a “color picker” I could plug into my sample app, and I did find a number of options (here’s one example), but none of them gave me exactly what I was looking for.
The closest fit for my needs was a terrific bit of code that somebody with the user name “StarCrunch” contributed to the Code Exchange last year. StarCrunch’s code was pretty darn close to what I envisioned – and most importantly, he or she had already worked out the math used to power a visual color picker, something I had no interest in doing from scratch. However, there were a few issues I needed to fix in order to fully meet my needs:
- I needed Graphics 2.0-compatible code (this is especially important for anything dealing with color, since RGB values are no longer on a 0-255 scale)
- I needed the picker to “return” the RGBA values for a picked color so that something could be done with them.
- StarCrunch’s code calculated the hex code for a picked color and displayed it on-screen. This is useful information, but just not something I needed.
- I needed a way for the user to adjust the alpha value of the picked color using their finger.
- I wanted to remove or hide the picker after the user had picked their color.
- I wanted my picker set up as an external module that could simply be included into any existing project (for easy “one line of code” integration).
So I set to work creating my own color picker, with StarCrunch’s excellent code to lead the way. The result is colorPicker.lua, my own color picker module that can be added to any Corona project with just one line of code! READ MORE »