Пример #1
0
        public static async Task <List <Color> > GetColorPalette(Uri path)
        {
            if (path == null)
            {
                return(new List <Color>()
                {
                    new UISettings().GetColorValue(UIColorType.Accent)
                });
            }
            //get the file
            if (path.IsFile)
            {
                var file = await StorageFile.GetFileFromPathAsync(path.LocalPath);

                using (IRandomAccessStream stream = await file.OpenReadAsync())
                {
                    //Create a decoder for the image
                    var decoder = await BitmapDecoder.CreateAsync(stream);

                    return((await colorThief.GetPalette(decoder, 4)).Select(a => FromColorThief(a.Color)).ToList());
                }
            }
            else
            {
                var random = RandomAccessStreamReference.CreateFromUri(path);
                using (IRandomAccessStream stream = await random.OpenReadAsync())
                {
                    //Create a decoder for the image
                    var decoder = await BitmapDecoder.CreateAsync(stream);

                    return((await colorThief.GetPalette(decoder, 4)).Select(a => FromColorThief(a.Color)).ToList());
                }
            }
        }
Пример #2
0
        public static async Task <List <ColorThiefDotNet.QuantizedColor> > GeneratePalette(Uri path)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }
            //get the file
            if (path.IsFile)
            {
                var file = await StorageFile.GetFileFromPathAsync(path.LocalPath);

                using (IRandomAccessStream stream = await file.OpenReadAsync())
                {
                    var decoder = await BitmapDecoder.CreateAsync(stream);

                    return(await colorThief.GetPalette(decoder, 8, 1, false));
                }
            }
            else
            {
                try
                {
                    RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(path);
                    using (IRandomAccessStream stream = await random.OpenReadAsync())
                    {
                        var decoder = await BitmapDecoder.CreateAsync(stream);

                        return(await colorThief.GetPalette(decoder, 8, 1, false));
                    }
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
Пример #3
0
        public static async Task <List <Color> > GetColorPalette(Uri path)
        {
            if (path == null)
            {
                return(new List <Color>()
                {
                    new UISettings().GetColorValue(UIColorType.Accent)
                });
            }
            //get the file
            if (path.IsFile)
            {
                var file = await StorageFile.GetFileFromPathAsync(path.LocalPath);

                using (IRandomAccessStream stream = await file.OpenReadAsync())
                {
                    //Create a decoder for the image
                    var decoder = await BitmapDecoder.CreateAsync(stream);

                    ////Create a transform to get a 1x1 image
                    //var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };

                    ////Get the pixel provider
                    //var pixels = await decoder.GetPixelDataAsync(
                    //    BitmapPixelFormat.Rgba8,
                    //    BitmapAlphaMode.Ignore,
                    //    myTransform,
                    //    ExifOrientationMode.IgnoreExifOrientation,
                    //    ColorManagementMode.DoNotColorManage);

                    ////Get the bytes of the 1x1 scaled image
                    //var bytes = pixels.DetachPixelData();

                    return((await colorThief.GetPalette(decoder, 4)).Select(a => FromColorThief(a.Color)).ToList());

                    //read the color
                    //return Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
                }
            }
            else
            {
                RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(path);
                using (IRandomAccessStream stream = await random.OpenReadAsync())
                {
                    //Create a decoder for the image
                    var decoder = await BitmapDecoder.CreateAsync(stream);

                    ////Create a transform to get a 1x1 image
                    //var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };

                    ////Get the pixel provider
                    //var pixels = await decoder.GetPixelDataAsync(
                    //    BitmapPixelFormat.Rgba8,
                    //    BitmapAlphaMode.Ignore,
                    //    myTransform,
                    //    ExifOrientationMode.IgnoreExifOrientation,
                    //    ColorManagementMode.DoNotColorManage);

                    ////Get the bytes of the 1x1 scaled image
                    //var bytes = pixels.DetachPixelData();

                    return((await colorThief.GetPalette(decoder, 4)).Select(a => FromColorThief(a.Color)).ToList());

                    //read the color
                    //return Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string file      = "";
            Plane  plane     = new Plane();
            int    n_row     = 0;
            int    n_col     = 0;
            double width     = 0;
            double height    = 0;
            int    precision = 0;

            Bitmap bm = null;
            List <ColorThiefDotNet.Color> palette = null;
            int interval     = 5;
            int paletteCount = 5;

            DA.GetData("File", ref file);
            DA.GetData("Plane", ref plane);
            DA.GetData("Num_Row", ref n_row);
            DA.GetData("Num_Col", ref n_col);
            DA.GetData("Width", ref width);
            DA.GetData("Height", ref height);
            DA.GetData("Precision", ref precision);
            DA.GetData("PaletteCount", ref paletteCount);
            if (precision < 1)
            {
                precision = 1;
            }
            if (precision > 10)
            {
                precision = 10;
            }

            DataTree <Plane>       plns  = new DataTree <Plane>();
            DataTree <Rectangle3d> rects = new DataTree <Rectangle3d>();
            DataTree <GH_Colour>   avgCs = new DataTree <GH_Colour>();
            DataTree <GH_Colour>   matCs = new DataTree <GH_Colour>();
            List <GH_Colour>       pltCs = new List <GH_Colour>();

            try
            {
                bm = new Bitmap(file);
                ColorThiefDotNet.ColorThief ct = new ColorThiefDotNet.ColorThief();
                palette  = ct.GetPalette(bm, paletteCount, 10, false).Select(e => e.Color).ToList();
                interval = (int)Math.Pow((bm.Width * bm.Height) / (n_col * n_row), 0.5) / precision;
                foreach (var c in palette)
                {
                    Color color = Color.FromArgb(c.R, c.G, c.B);
                    pltCs.Add(new GH_Colour(color));
                }
            }
            catch { }

            for (int i = 0; i < n_col; i++)
            {
                for (int j = 0; j < n_row; j++)
                {
                    GH_Path  path = new GH_Path(i);
                    Vector3d tx   = plane.XAxis * i * width;
                    Vector3d ty   = plane.YAxis * j * height;
                    Vector3d t    = tx + ty;
                    Plane    pln  = plane.Clone();
                    pln.Translate(t);
                    plns.Add(pln, path);

                    Rectangle3d rect = new Rectangle3d(pln, width, height);
                    rects.Add(rect, path);

                    if (bm != null)
                    {
                        int x       = (int)(Convert.ToDouble(bm.Width) / n_col * (i + 0.5));
                        int y       = (int)(Convert.ToDouble(bm.Height) / n_row * (j + 0.5));
                        int mWidth  = bm.Width / n_col;
                        int mHeight = bm.Height / n_row;


                        Color     avgColor = GetAveragedColor(bm, x, y, mWidth, mHeight, interval);
                        GH_Colour avgC     = new GH_Colour(avgColor);
                        avgCs.Add(avgC, path);

                        var       tempC    = GetPaletteClosest(palette, avgColor);
                        Color     matColor = Color.FromArgb(tempC.R, tempC.G, tempC.B);
                        GH_Colour matC     = new GH_Colour(matColor);
                        matCs.Add(matC, path);
                    }
                }
            }

            DA.SetDataTree(0, plns);
            DA.SetDataTree(1, rects);
            DA.SetDataTree(2, avgCs);
            DA.SetDataTree(3, matCs);
            DA.SetDataList(4, pltCs);
        }