28 Replies Latest reply on Nov 14, 2009 11:01 AM by Devoteé

    fill an object by it's color name?

    Devoteé Level 1

      I've created a colour palette and given each colour a unique number and name (E.g. V123 Plum, V456 Ruby). Also the colour names is being loaded from a mysql database.

       

      When the name of the colour is loaded in to a dynamic box , I want to fill an object in that colour.

       

      var mycol1 = V123 Plum;
      V123 Plum = "0xFF6600"
      colorchange.setRGB(mycol1);

       

      var mycol2 = V456 Ruby;

      V123 Plum = "0xFFCC00"
      colorchange.setRGB(mycol2);

        • 1. Re: fill an object by it's color name?
          kglad Adobe Community Professional & MVP

          use:

           


          V123 Plum = "0xFF6600"

          var colorchange:Color=new Color(yourobject);
          colorchange.setRGB(V123Plum);

           


          • 2. Re: fill an object by it's color name?
            Devoteé Level 1

            Thank you! I'll use it and come back to mark the thread as the correct answer! 

            • 3. Re: fill an object by it's color name?
              Devoteé Level 1

              There's a error!

               

              CODE:

              //colours and names in the palette

              V123 Plum = "0xFF6600";

              V456 Blue = "0xFF0000";

              V789 Dark = "0x000000";

              // so on...

              //below are my object instances
              var colorchange:Color=new Color(A8);

              var colorchange:Color=new Color(B8);

              var colorchange:Color=new Color(C8);
              colorchange.setRGB(V123 Plum);

              colorchange.setRGB(V456 Blue);

              colorchange.setRGB(V789 Dark);
                
              var senderLoad:LoadVars = new LoadVars();
              var receiveLoad:LoadVars = new LoadVars();

              //continues...

              • 4. Re: fill an object by it's color name?
                kglad Adobe Community Professional & MVP

                don't use the same name for different color instances:

                 


                //colours and names in the palette

                V123 Plum = "0xFF6600";

                V456 Blue = "0xFF0000";

                V789 Dark = "0x000000";

                // so on...

                //below are my object instances
                var colorchange1:Color=new Color(A8);

                var colorchange2:Color=new Color(B8);

                var colorchange3:Color=new Color(C8);
                colorchange1.setRGB(V123 Plum);

                colorchange2.setRGB(V456 Blue);

                colorchange.3setRGB(V789 Dark);
                  
                var senderLoad:LoadVars = new LoadVars();
                var receiveLoad:LoadVars = new LoadVars();

                //continues...

                • 5. Re: fill an object by it's color name?
                  Devoteé Level 1

                  There are a syntax errors.

                   

                  V123 Plum = "0xFF6600";- I think it's due to the spaces between the words (V123 PLUM)

                  var colorchange1:Color=new Color(A8);  - '/' or ',' expected

                   

                  How to solve this and make the code working?

                  • 6. Re: fill an object by it's color name?
                    kglad Adobe Community Professional & MVP

                    i assumed those spaces were forum artifacts.  you can't have any spaces in objects in actionscript (or any other programming language).

                    • 7. Re: fill an object by it's color name?
                      Devoteé Level 1

                      The text boxes will have the colour name with spaces. How can I deal with it? Can I write the script to use a part of the text.

                       

                      E.g. "V123 Plum" to use only V123 for identification and for "V456 Blue" to use only V456 while the text boxes display V123 Plum / V456 Blue?

                       

                      Like this?

                      //colours and names in the palette

                      V123 = "0xFF6600";

                      V456 = "0xFF0000";

                      V789 = "0x000000";

                      // so on...

                      //below are my object instances
                      var colorchange1:Color=new Color(A8);

                      var colorchange2:Color=new Color(B8);

                      var colorchange3:Color=new Color(C8);
                      colorchange1.setRGB(V123);

                      colorchange2.setRGB(V456);

                      colorchange.3setRGB(V789);
                        
                      var senderLoad:LoadVars = new LoadVars();
                      var receiveLoad:LoadVars = new LoadVars();

                      //continues...

                      • 8. Re: fill an object by it's color name?
                        kglad Adobe Community Professional & MVP

                        that's ok.

                        • 9. Re: fill an object by it's color name?
                          Devoteé Level 1

                          Here's what I've done so far. It's always display the last colour (000000) in B8. How can I  make it read the result (a[2]) and display the relevant colour?

                           

                          var senderLoad:LoadVars = new LoadVars();
                          var receiveLoad:LoadVars = new LoadVars();
                          receiveLoad.onData = function(src) {
                             var a:Array=src.split(",,");
                             result1.text=a[0];
                             result2.text=a[1];
                             CN1.text=a[2];
                             CN2.text=a[3];
                             CN3.text=a[4];
                             CN4.text=a[5];
                             CN5.text=a[6];
                             CN6.text=a[7];
                             CN7.text=a[8];
                             colorchange = new Color(B8);{
                          if (a[3]= "V123 WHITE"){
                          colorchange.setRGB(0x006666);
                          }
                          if (a[3]= "V456 BLUE"){
                          colorchange.setRGB(0x000000);}
                          }
                          };

                          • 10. Re: fill an object by it's color name?
                            kglad Adobe Community Professional & MVP

                            use double equal (==) to test for equality:

                             

                            Devoteé wrote:

                             

                            Here's what I've done so far. It's always display the last colour (000000) in B8. How can I  make it read the result (a[2]) and display the relevant colour?

                             

                            var senderLoad:LoadVars = new LoadVars();
                            var receiveLoad:LoadVars = new LoadVars();
                            receiveLoad.onData = function(src) {
                               var a:Array=src.split(",,");
                               result1.text=a[0];
                               result2.text=a[1];
                               CN1.text=a[2];
                               CN2.text=a[3];
                               CN3.text=a[4];
                               CN4.text=a[5];
                               CN5.text=a[6];
                               CN6.text=a[7];
                               CN7.text=a[8];
                               colorchange = new Color(B8);{
                            if (a[3]== "V123 WHITE"){
                            colorchange.setRGB(0x006666);
                            }
                            if (a[3]== "V456 BLUE"){
                            colorchange.setRGB(0x000000);}
                            }
                            };

                            • 11. Re: Thanks! but an additonal question
                              Devoteé Level 1

                              Yes it works!!  Thank you kglad. 

                               

                              You answered my question but could you please tell me how to write this code in a simpler way (shortened way)??? Because I have 100 colours in my palette and six objects which the colours to be applied!!! if not I'll have to repeat the "if" statemnt 600 times.

                               

                              V123 WHITE, V456 FOREST, V789 RUBY .... //100 colours

                              B8, C8, D8... // 6 objects

                              • 12. Re: Thanks! but an additonal question
                                kglad Adobe Community Professional & MVP

                                i see the logic in

                                 

                                   CN1.text=a[2];
                                   CN2.text=a[3];
                                   CN3.text=a[4];
                                   CN4.text=a[5];
                                   CN5.text=a[6];
                                   CN6.text=a[7];
                                   CN7.text=a[8];

                                 

                                so, that can be written concisely.  but i don't see the logic in those if-statements.  is there deterministic logic behind that?

                                • 13. Re: Thanks! but an additonal question
                                  Devoteé Level 1

                                  Imagine a lineart of a tree with fruits. I've assigned B8 to the leaves, C8 to the body and D8 to the fruits. A viewer selects gold for the leaves, black for the body of the tree and purple for the furits. I need to display the colour name of each part and make the name match a colour value to be applied to the  colour swatch besides each text box. Then I have to write a colour value defining each colour name under the "if" statement and also I have to duplicate the code to be applied to the B8, C8 and D8. SO thats number_of_sections_in_the_tree X number_of_colours

                                   

                                  Basically in my FLA I've got 6 dyn. text boxes where the name of the colour appears and just next to it a box for each dyn. text box to display the actual colour visually. The logic behind the if statement is to match the colour name and output its colour.

                                   

                                  But instead of repeating the complete if statment for 600 times I need a way to write the script in a shortened way. something like this,

                                   

                                  I got 100 colours in my palette.

                                  B8 is the blank colour swatch. (there are other blank watches called C8, D8, E8 and F8)

                                  CN1, CN2 ... are the text boxes which receives th coloour value from the db (V123 PLUM, V456 RUBY, ...)

                                  • 14. Re: Thanks! but an additonal question
                                    kglad Adobe Community Professional & MVP

                                    let's try it this way:

                                     

                                    given a value of i and given a[i]'s string value, how do you determine which movieclip (B8, C8 etc) should be colored and how do you determine which of your 100 color's to use.

                                    • 15. Re: Thanks! but an additonal question
                                      Devoteé Level 1

                                      Each movieclip is in seperate frames. (B8 is in frame 1, C8 is in frame 2, so on...)

                                      • 16. Re: Thanks! but an additonal question
                                        kglad Adobe Community Professional & MVP

                                        are you coloring all objects using a color chosen randomly?

                                        • 17. Re: Thanks! but an additonal question
                                          Devoteé Level 1

                                          No, it's not a random colour. User is able to select colours for each movieclip (B8, C8, D8, etc...)

                                           

                                          E.g. Imagine it's a line-drawing of a tree. When the user clicks the leaves and click on a colour on the palette the selected colour will be applied to the leaves only. If the viwer clicks on the fruits and click on a colour in the palettte the selected colour will be applied to the fruits only. etc...

                                          • 18. Re: Thanks! but an additonal question
                                            kglad Adobe Community Professional & MVP

                                            create variables, selectedItem and selectedColor.  when a part is clicked, assign the selectedItem to be that part.  when a color is clicked assign the selectedItem's color to be the selected color.

                                             

                                            p.s.  each color swatch has a property that references that swatch'es color, correct?

                                            • 19. Re: Thanks! but an additonal question
                                              Devoteé Level 1

                                              At start how could I make the boxes fill its default colour by matching its name? please show me a sample code who to do it.

                                               

                                              Eg. The same example of the tree. When it loads, the leaves text box will display GREEN and the fruits will display ORANGE. As wel as the sample colour display box will automatically match the colour value by the name appearing above (green, orange..)

                                              • 20. Re: Thanks! but an additonal question
                                                kglad Adobe Community Professional & MVP

                                                you need to code something that relates the string GREEN to the color you want to use.

                                                • 21. Re: Thanks! but an additonal question
                                                  Devoteé Level 1

                                                  That's already being done. But I need to shorten the code to avoid repeating the IF 600 times (100 colours & 6 objects)

                                                   

                                                  //objects reffered as B8, C8, D8, E8,etc...

                                                  //colours reffered by V123 WHITE, 456 RUBY, etc...

                                                  //CN1, CN2, CN3 will pull the defaul names for each section when te page loads.

                                                   

                                                     var a:Array=src.split(",,");
                                                     result1.text=a[0];
                                                     result2.text=a[1];
                                                     CN1.text=a[2];
                                                     CN2.text=a[3];
                                                     CN3.text=a[4];
                                                     CN4.text=a[5];
                                                     CN5.text=a[6];
                                                     CN6.text=a[7];
                                                     CN7.text=a[8];
                                                     CN11.text=CN1.text;
                                                     CN22.text=CN2.text;
                                                     CN33.text=CN3.text;
                                                     CN44.text=CN4.text;
                                                     CN55.text=CN5.text;
                                                     CN66.text=CN6.text;
                                                     CN77.text=CN7.text;

                                                   

                                                     colorchange = new Color(B8);{
                                                  if (a[3]== "V123 WHITE"){
                                                  colorchange.setRGB(0x000000);
                                                  }

                                                  • 22. Re: Thanks! but an additonal question
                                                    kglad Adobe Community Professional & MVP

                                                    you need to do something to relate strings to color numbers like:

                                                     

                                                    var colorObj:Object={}l

                                                    colorObj["GREEN"]=0x00ff00;

                                                    etc.

                                                     

                                                    to avoid using those if-statements.

                                                    1 person found this helpful
                                                    • 23. Re: Thanks! but an additonal question
                                                      Devoteé Level 1

                                                      I didn't exactly get it. Did you mean something like this?

                                                       

                                                      var colorObj:Object={}l
                                                      colorObj["V123 WHITE"]=0x006666;
                                                      var colorObj:Object={}2
                                                      colorObj["V456 BLUE"]=0x000000;

                                                       

                                                         colorchange = new Color(B8);{
                                                          colorchange.setRGB(colorObj);
                                                          };

                                                      • 24. Re: Thanks! but an additonal question
                                                        Devoteé Level 1

                                                        I didn't exactly get it. Did you mean something like this?

                                                         

                                                        var colorObj:Object={}l
                                                        colorObj["V123 WHITE"]=0x006666;
                                                        var colorObj:Object={}2
                                                        colorObj["V456 BLUE"]=0x000000;

                                                         

                                                            colorchange = new Color(B8);{
                                                            colorchange.setRGB(colorObj);
                                                            };

                                                        • 25. Re: Thanks! but an additonal question
                                                          kglad Adobe Community Professional & MVP

                                                          this is good:

                                                           

                                                          var colorObj:Object={}l
                                                          colorObj["V123 WHITE"]=0x006666;
                                                          var colorObj:Object={}2
                                                          colorObj["V456 BLUE"]=0x000000;

                                                           

                                                          but then you would use the text property from the input text field:

                                                           

                                                          colorchange=new Color(selectedItem);  // where selectedItem is a variable indicating which items where selected - A8,B8 etc

                                                          colorchange.setRGB(colorObj[inputtextfield.text]);  // and inputtextfield is the textfield where users type V123 WHITE etc

                                                          • 26. Re: Thanks! but an additonal question
                                                            Devoteé Level 1

                                                            There no input text box here. The name of the colour is being pulled from a db.

                                                             

                                                            This is The code so far. It's not working also Flash syays thete are syntax errors in the code marked in red.

                                                             

                                                            var senderLoad:LoadVars = new LoadVars();
                                                            var receiveLoad:LoadVars = new LoadVars();
                                                            receiveLoad.onData = function(src) {
                                                               var a:Array=src.split(",,");
                                                               result1.text=a[0];
                                                               result2.text=a[1];
                                                               CN1.text=a[2];
                                                               CN2.text=a[3];
                                                               CN3.text=a[4];
                                                               CN4.text=a[5];
                                                               CN5.text=a[6];
                                                               CN6.text=a[7];
                                                               CN7.text=a[8];
                                                               CN11.text=CN1.text;
                                                               CN22.text=CN2.text;
                                                               CN33.text=CN3.text;
                                                               CN44.text=CN4.text;
                                                               CN55.text=CN5.text;
                                                               CN66.text=CN6.text;
                                                               CN77.text=CN7.text;

                                                            var colorObj:Object={}l
                                                            colorObj["C140 WHITE"]=0x006666;
                                                            var colorObj:Object={}2
                                                            colorObj["C267 BLUE"]=0x000000;

                                                             

                                                               colorchange = new Color(B8);{
                                                                colorchange.setRGB(colorObj[CN11.text]);
                                                               }

                                                               colorchange = new Color(C8);{
                                                                colorchange.setRGB(colorObj[CN22.text]);
                                                               }
                                                              };

                                                            • 27. Re: Thanks! but an additonal question
                                                              kglad Adobe Community Professional & MVP

                                                              remove the 1 and 2 next to the curly brackets.  those are forum artifacts.

                                                               

                                                              whatever the user selects needs to generate a string and that string needs to be used with colorObj to generate a color number.  if you use a colorpicker, that's done for you.

                                                              • 28. Re: Thanks! but an additonal question
                                                                Devoteé Level 1

                                                                My program works fine when the user clicks on any watch to apply the colur to any part of the drawing. My problem is at the begining (when the page first loads) the colour swatch to load the colour by the name of the colour.