I originally created this class for a plugin I was coding at the time (
ChatEdit). I wanted the option for players to be able to select a color in the most straight-forward and familiar way, by being presented with a color picker that they can click on.
The original version took the requested dimensions and produced 1x1 pixel controls to fill the area with the colors, taking a few seconds to load and producing thousands of event handlers. Of course this wasn't the most practical way, and thankfully
Garan stepped in converting the pixel controls to mouse co-ordinates that would map the color value, using the code that previously generated the pixels, and also placed a picker jpg as the background to display the colors.
The newer version from Garan took a lot less time to load as it was no longer producing thousands of controls, and to the user, produced the same end result.
However, the class was created for
ChatEdit so still required other coders to tinker with it a bit if they wanted to use it in their own plugins.
I have spent the past couple of days modifying the class again, this time so it can be used with ease in other plugins. In this version I've added in some mouse-click events and functions to retrieve the clicked color values, you no longer need to place this into the class itself, it can all be done from within your own script
Using the class..
First, make sure you copy the ColorPicker.lua file and the resource folder to your plugin directory.. You will then need to import the ColorPicker file into your script.
You will need to set the value for PICKER_JPG_DIR so it points to the location of picker.jpg in your project.
PICKER_JPG_DIR = "myDir/resources/picker.jpg";
You can then go about creating a color picker control..
myColourPicker = ColorPicker.
Create ();
myColourPicker:
SetParent ( control );
myColourPicker:
SetSize ( width, height );
myColourPicker:
SetPosition ( left, top );
Each color picker control has three mouse-click events you can customise, these are when the mouse is left-clicked, right-clicked, or another mouse button is clicked.
myColourPicker
.LeftClick = function () -- Left-click mouse event
myColourPicker
.RightClick = function () -- Right-Click mouse event
myColourPicker
.OtherClick = function () -- Other mouse button click event
Each mouse-click creates 3 possible outputs, a Turbine Color Class, the Hex value, and the RGB values of the color clicked. To access these use:
myColourPicker
:GetTurbineColor (); - returns a Turbine color class
myColourPicker
:GetHexColor (); -- returns the hex value e.g. FFFFFF
myColourPicker
:GetRGBColor (); -- returns three number values, red, green, and blue
Known Issues:
-The class is able to rescale picker.jpg's width only due to problems with Turbines stretch mode property. The default height is 70 pixels, if you want to make it bigger or smaller you will need to first modify picker.jpg externally to change the height. When you create the picker in your script make sure you SetHeight() to the same value otherwise the mapping will be off.