Hi, My example is very simple and I think that it will be very usefull for you. An example can be view at http://www.extreme-design.info/tutorial/color.html And you can download the full source from http://www.extreme-design.info/tutorial/color.swi You can use it for your website to change background color for example.
 So I will explain you how I made this aplication. First you can draw a rectangle, enter a text box for the tile for example “Color composer”. This doesn’t matter.
 Then insert 4 text box. The first three textbox must be set to “Input text” and you can name them “red_dec”, “green_dec” and “blue_dec”. I named them like that beacause I will keep in them numbers in base 10 from 0 to 255.
 Don’t forget to select “Target” check box and set the variable name of the text box. I used red, green and blue. You have to enter then the “Maximum charecters allowed” that will be = 3 because the maximum accepted digits is equal with 3 (255). This stepts for text were the most important, now you can set the dimensions of the text, aply a border to the text box etc. After doing this you need to set up the textbox number 4. You can name it “hexcode”. In this textbox it will be displayed the hexadecimal code for the current color.This text box must be set as a “Dynamic text”, then check “Target” and enter variable name “color_hex”. The hexadecimal number will be created by converting the decimal numbers keeped in variables “red”, “green” and “blue” in haxadecimal. Now lets create some buttons.
 Draw 2 buttons near the each input text box.. These buttons will be used to increase and to subtract the decimal color number. Now you have to enter a script. When a button will be pressed a variable for example “b_red_p” will be set “true” else “false”. “b_red_p” is for increasing the decimal red color number.
 I created a function to convert each decimal number in hexadecimal. Hexadecimal is a number in base 16. To convert a decimal number to hexadecimal you have to divide the decimal number with 16. The floor number resulted will be replaced with numbers between 0-9 if the number is <10 and with characters from “a” to “f” if the number is between 10-15. The rest will be replaced like the floor number and we will obtain a hexadecimal number by adding them. So here is the function:
| CODE | function color_f(send_color) { if (send_color=="") { send_color=0; } cl=Math.floor(send_color/16); cl_rest=send_color % 16; if (cl==10) { cl="a"; } if (cl==11) { cl="b"; } if (cl==12) { cl="c"; } if (cl==13) { cl="d"; } if (cl==14) { cl="e"; } if (cl==15) { cl="f"; } if (cl_rest==10) { cl_rest="a"; } if (cl_rest==11) { cl_rest="b"; } if (cl_rest==12) { cl_rest="c"; } if (cl_rest==13) { cl_rest="d"; } if (cl_rest==14) { cl_rest="e"; } if (cl_rest==15) { cl_rest="f"; } return new_hex_color=cl add cl_rest; } |
“send_color” is the decimal number for the color that will be changed. The funcyion will return “new_hex_color” that is the decimal number converted to hexadecimal. I created a sprite “box” where I draw 2 shapes. Each new hexadecimal code will be applied as a rgb color for that sprite. The sintax is colorObject.setRGB(0xRRGGBB); 0xRRGGBB; defines either the hexadecimal or Red/Green/Blue color to be set. RR, GG, and BB need to be two hexidecimal digits each. 0x tells the script that the number is hexidecimal. Example:
| CODE | onLoad () { myColor = new Color(mySprite); myColor.setRGB(0xFF6600); } |
This sets the color object to a value of Red=255, Green=102, Blue=0 or #FF6600 (Orange)
So I used the next code:
| CODE | onLoad () { red=0; green=0; blue=0; c = new Color(box); } onEnterFrame() { color_hex=color_f (red) add color_f (green) add color_f (blue); color_hex_box="0x" add color_f (red) add color_f (green) add color_f (blue); c.setRGB(color_hex_box); if (b_red_p eq "true") { if (red<255) { red++; } } if (b_red_m eq "true") { if (red>0) { red--; } } if (b_green_p eq "true") { if (green<255) { green++; } } if (b_green_m eq "true") { if (green>0) { green--; } } if (b_blue_p eq "true") { if (blue<255) { blue++; } } if (b_blue_m eq "true") { if (blue>0) { blue--; } } } |
This were the most important steps. After I finished this I created a new sprite named “colors” and I added some rectangles with different colors. On each rectangle selft event I used this code that is for white color:
| CODE | onSelfEvent (press) { _root.colorchange.red=255; _root.colorchange.green=255; _root.colorchange.blue=255; } |
The numbers were changed to obtain diferent colors combinations.
This is my tutorial. I hope you’ll like it. I’m not very good on english so I didn’t explained it very well. Sorry for my bad english.
Good Luck to everyone, Regards, Ovidiu
|