Exemplo n.º 1
0
        /// <summary>
        /// Extracts from colorlovers, the percentages only the pixel-width, so it as to be divied with the totalwidth
        /// </summary>
        /// <returns>The from colorlovers.</returns>
        /// <param name="doc">Document.</param>
        /// <param name="loadPercentages">If set to <c>true</c> load percentages.</param>
        public static PaletteData extractFromColorlovers(Document doc, bool loadPercentages)
        {
            int         colorCount   = 0;
            int         percentCount = 0;
            PaletteData palette      = new PaletteData();

/*
 *                                              IEnumerable<Tag> headerTags = doc.FindAll ("h1");
 *                                              foreach (Tag headerTag in headerTags) {
 * //								if (!string.IsNullOrEmpty (headerTag.c)) {
 *
 *
 *                                                              //palette.name = headerTag.ToString ();
 *                                                              Debug.Log (headerTag.ToString ().HtmlDecode ());
 *                                                              //["class"] == "feature-detail-container") {
 *
 * //								}
 *                                              }
 */


            IEnumerable <Tag> links = doc.FindAll("a");


            foreach (Tag a in links)
            {
                if (a ["class"] == "left pointer block")
                {
                    string style = a ["style"];
                    //Debug.Log ("style.Split (';') " + style.Split (';').Length);

                    foreach (string styleCss in style.Split(';'))
                    {
                        if (loadPercentages && styleCss.Contains("width"))
                        {
                            string width = styleCss.Split(':') [1];
                            width = width.Substring(0, width.IndexOf("px"));
                            float widthF = float.Parse(width.Trim());

                            //Debug.Log ("found % " + widthF + " from " + styleCss);

                            palette.totalWidth += widthF;
                            palette.percentages [percentCount++] = widthF;
                        }
                        else if (styleCss.Contains("background-color"))
                        {
                            string bgColor = styleCss.Split(':') [1];
                            bgColor = bgColor.Trim().Substring(1);
                            palette.colors [colorCount++] = JSONPersistor.HexToColor(bgColor);
                        }
                    }

                    //Debug.Log (style);
                }
            }

//						Debug.Log (palette.percentages [0]);

            return(palette);
        }
Exemplo n.º 2
0
        public bool ImportPaletteCollection(string newURL)
        {
            reset();

            analizeURL(newURL);

            WWW html = new WWW(newURL);

            while (!html.isDone)
            {
                if (!string.IsNullOrEmpty(html.error))
                {
                    throw new UnityException("error loading URL: " + html.error);
                }
            }

            Debug.Log("download finished, loaded " + html.bytesDownloaded + " bytes");

            collectionData.paletteURL = newURL;

            HtmlParser parser = new HtmlParser();
            Document   doc    = parser.Parse(html.text);

            //Debug.Log (doc.ToString ());

            PaletteData extracedData = null;

            if (isColourLovers)
            {
                extracedData = PaletteImporter.extractFromColorlovers(doc, this.collectionData.loadPercent);

                if (this.collectionData.loadPercent)
                {
                    for (int i = 0; i < extracedData.percentages.Length; i++)
                    {
                        // totalWidth = 100% this.myData.percentages [i] = x%
                        extracedData.percentages [i] = extracedData.percentages [i] / extracedData.totalWidth;
                    }
                }
                else
                {
                    extracedData.percentages = PaletteData.getDefaultPercentages();
                }
            }
            else if (isPLTTS)
            {
                extracedData = PaletteImporter.extractFromPLTTS(doc, this.collectionData.loadPercent);
            }

/*						if (extracedData != null) {
 *                                                              return CreatePalette (new KeyValuePair<string, PaletteData> (extracedData.name, extracedData));
 *                                              } else {
 *                                                              Debug.Log ("Palette :'" + extracedData.percentages [0] + "' could not be load... is the URL correct? ");
 *                                                              return false;
 *                                              }
 */
            return(this.collectionData.setSize(this.collectionData.palettes.Count + 1,
                                               new KeyValuePair <string, PaletteData> (extracedData.name, extracedData)));
        }
Exemplo n.º 3
0
        public static PaletteData getInstance(JSONClass jClass)
        {
            PaletteData palette = new PaletteData();

            //Debug.Log ("getInstance: " + jClass.ToString ());
            palette.setPalette(jClass);
            return(palette);
        }
Exemplo n.º 4
0
 new public void init()
 {
     if (myData == null)
     {
         myData = new PaletteData();
         base.init();
     }
 }
        public virtual void setPalette(JSONClass jClass)
        {
            name = jClass ["collectionName"];

            this.paletteURL  = jClass ["paletteURL"];
            this.loadPercent = jClass ["loadPercent"].AsBool;

            //Debug.Log (" " + jClass ["palettes"].ToString ());

            foreach (string key in jClass["palettes"].Keys)
            {
                this.palettes.Add(key, PaletteData.getInstance(jClass ["palettes"] [key].AsObject));
            }
        }
Exemplo n.º 6
0
        public static PaletteData extractFromPLTTS(Document doc, bool loadPercent)
        {
            int         colorCount   = 0;
            int         percentCount = 0;
            PaletteData palette      = new PaletteData();

            Tag colorBlock = doc.Find(".palette-colors");

            //Debug.Log (colorBlock);

            foreach (Element colorDiv in colorBlock.Children)
            {
                if (!string.IsNullOrEmpty(colorDiv.ToString().Trim()))
                {
                    // can contain empty Elements!

                    //Tag colorTag = (HtmlSharp.Elements.Tags.Div)colorDiv;
                    Tag colorTag = (Tag)colorDiv;

                    string style = colorTag ["style"];
                    foreach (string styleCss in style.Split(';'))
                    {
                        if (loadPercent && styleCss.Contains("width"))
                        {
                            string width = styleCss.Split(':') [1];
                            width = width.Substring(0, width.IndexOf("%"));
                            float widthF = float.Parse(width.Trim());
                            palette.totalWidth += widthF / 100;
                            palette.percentages [percentCount++] = widthF / 100;
                        }
                        else if (styleCss.Contains("background-color"))
                        {
                            string bgColor = styleCss.Split(':') [1];
                            bgColor = bgColor.Trim().Substring(1);
                            palette.colors [colorCount++] = JSONPersistor.HexToColor(bgColor);
                        }
                    }

                    //Debug.Log (style);
                }
            }

            if (!loadPercent)
            {
                palette.percentages = PaletteData.getDefaultPercentages();
            }

            return(palette);
        }
Exemplo n.º 7
0
        private IEnumerator ImportPaletteFromURL(string newURL)
        {
            analizeURL(newURL);

            WWW html = new WWW(newURL);

            while (!html.isDone)
            {
                if (!string.IsNullOrEmpty(html.error))
                {
                    throw new UnityException("error loading URL: " + html.error);
                }

                //Debug.Log ("downloading Palette " + html.progress + "%");
            }

            Debug.Log("download finished, loaded " + html.bytesDownloaded + " bytes");
            this.myImporterData.paletteURL = newURL;
            string[] splitted    = newURL.Split('/');
            string   paletteName = splitted [splitted.Length - 1];


            HtmlParser parser = new HtmlParser();
            Document   doc    = parser.Parse(html.text);

/*						this.myImporterData.colors = new Color[5];
 *                                              this.myImporterData.percentages = new float[5];
 */
            PaletteData extracedData = null;

            if (isColourLovers)
            {
                extracedData = extractFromColorlovers(doc, this.myImporterData.loadPercent);

                // don't copy directly because of PaletteData vs PaletteImporterData
                this.myImporterData.totalWidth = extracedData.totalWidth;
                this.myImporterData.name       = paletteName;
                this.myImporterData.colors     = extracedData.colors;
                this.myImporterData.alphas     = extracedData.alphas;


                if (this.myImporterData.loadPercent)
                {
                    this.myImporterData.percentages = extracedData.percentages;


                    for (int i = 0; i < this.myImporterData.percentages.Length; i++)
                    {
                        // totalWidth = 100% this.myData.percentages [i] = x%

                        this.myImporterData.percentages [i] = this.myImporterData.percentages [i] / this.myImporterData.totalWidth;
                        //Debug.Log ("totalWidth " + this.myImporterData.totalWidth + " % " + this.myImporterData.percentages [i]);
                    }
                }
                else
                {
                    this.myImporterData.percentages = PaletteData.getDefaultPercentages();
                }
            }
            else if (isPLTTS)
            {
                extracedData = extractFromPLTTS(doc, this.myImporterData.loadPercent);

                this.myImporterData.name        = paletteName;
                this.myImporterData.colors      = extracedData.colors;
                this.myImporterData.percentages = extracedData.percentages;
                this.myImporterData.alphas      = extracedData.alphas;
            }

            yield return(null);
        }