Example #1
0
        private static Color Mix(WeightedValue<Color>[] colors)
        {
            double weightSum = colors.Select(vv => vv.Weight).Sum();

            var normalizedHSLColors = colors.Select(vc => new WeightedValue<HSL>(ColorSpaceHelper.RGBtoHSL(vc.Value.R, vc.Value.G, vc.Value.B), vc.Weight / weightSum));

            double h = 0.0;
            double count = 0.0;
            double sSum = 0.0;
            double lSum = 0.0;
            double x = 0.0;
            double y = 0.0;

            foreach (var color in normalizedHSLColors)
            {
                count++;
                sSum += color.Value.Saturation * (color.Weight * 2.0);
                lSum += color.Value.Luminance * (color.Weight * 2.0);
                x += Math.Cos(2.0 * Math.PI * (DegreeToPercent(color.Value.Hue) * (color.Weight * 2.0)));
                y += Math.Sin(2.0 * Math.PI * (DegreeToPercent(color.Value.Hue) * (color.Weight * 2.0)));
            }

            double s = sSum / count;
            double l = lSum / count;

            if ((x != 0.0) || (y != 0.0))
            {
                h = Math.Atan2(y, x) / (2.0 * Math.PI);
            }
            else
            {
                s = 0.0;
            }

            var result = ColorSpaceHelper.HSLtoRGB(PercentToDegree(h), s, l);

            double r = result.Red, g = result.Green, b = result.Blue;

            const double addRatio = 0.0;
            const double avgRatio = 0.0;
            const double subRatio = 0.0;
            const double colorizeRatio = (1.0 - (addRatio + avgRatio + subRatio));

            double additiveR = ComputeAdditiveComponent(colors, weightSum, (c) => c.R);
            double substractiveR = ComputeSubtractiveComponent(colors, weightSum, (c) => c.R);
            double avgR = ComputeAvgComponent(colors, weightSum, (c) => c.R);

            double additiveG = ComputeAdditiveComponent(colors, weightSum, (c) => c.G);
            double substractiveG = ComputeSubtractiveComponent(colors, weightSum, (c) => c.G);
            double avgG = ComputeAvgComponent(colors, weightSum, (c) => c.G);

            double additiveB = ComputeAdditiveComponent(colors, weightSum, (c) => c.B);
            double substractiveB = ComputeSubtractiveComponent(colors, weightSum, (c) => c.B);
            double avgB = ComputeAvgComponent(colors, weightSum, (c) => c.B);

            return Color.FromRgb(
                ToByte(r * colorizeRatio + additiveR * addRatio + substractiveR * subRatio + avgR * avgRatio),
                ToByte(g * colorizeRatio + additiveG * addRatio + substractiveG * subRatio + avgG * avgRatio),
                ToByte(b * colorizeRatio + additiveB * addRatio + substractiveB * subRatio + avgB * avgRatio));
        }
Example #2
0
        WeightedValue GenerateRandomWeightedValue(IEnumerable <WeightedValue> values, int count)
        {
            if (count == 0)
            {
                return(null);
            }
            var totalWeight = values.Sum(v => v.weight + (v.TimesNotSelected * MissMultiplier));

            var randomValue = UnityEngine.Random.Range(0f, totalWeight);

            WeightedValue selectedValue = null;

            foreach (var option in values)
            {
                var optionWeight = option.weight + (option.TimesNotSelected * MissMultiplier);
                if (randomValue <= optionWeight)
                {
                    selectedValue = option;
                    option.TimesSelected++;
                }
                else
                {
                    option.TimesNotSelected++;
                }
                randomValue -= optionWeight;
            }
            return(selectedValue);
        }
Example #3
0
        public void AdjustNegativeWeightsWithPercentage()
        {
            var value = new WeightedValue(new IValue[] { new RealValue(1), new RealValue(2) }, new double[] { -1, -1 });

            value.Adjust(0.1);

            Assert.IsTrue(value.Weights.All(w => w == -0.9));
        }
Example #4
0
        public IEnumerator CreatePolygon(List <FieldConnectPoint> points, float width, float uvY1, float uvY2)
        {
            var candidates = new WeightedValue[] { new WeightedValue {
                                                       value = width, weight = 1
                                                   } };

            yield return(CoroutineUtility.CoroutineCycle(CreatePolygon(points, candidates, uvY1, uvY2)));
        }
Example #5
0
    public PlanetProperty(double value, float variation)
    {
        Value     = value;
        Variation = variation;

        for (int i = 0; i <= 100; i++)
        {
            HexDistribution[i] = new WeightedValue(PPMath.Round(i / 100f), 0);
        }
    }
Example #6
0
        public WeightedValue AddWeightedOption(T value, float weight = 1f)
        {
            var newVal = new WeightedValue
            {
                value  = value,
                weight = weight
            };

            options.Add(newVal);

            return(newVal);
        }
Example #7
0
        public void CreatedWeightedValueWithValueThree()
        {
            var value = new WeightedValue(new IValue[] { new RealValue(1), new RealValue(2) }, new double[] { 1, 1 });

            Assert.IsNotNull(value.Inputs);
            Assert.AreEqual(2, value.Inputs.Count);

            Assert.IsNotNull(value.Weights);
            Assert.AreEqual(2, value.Weights.Count);

            Assert.AreEqual(3, value.Value);
        }
Example #8
0
        public void CreatedWeightedValueWithValueZero()
        {
            var value = new WeightedValue(new IValue[] { }, new double[] { });

            Assert.IsNotNull(value.Inputs);
            Assert.AreEqual(0, value.Inputs.Count);

            Assert.IsNotNull(value.Weights);
            Assert.AreEqual(0, value.Weights.Count);

            Assert.AreEqual(0, value.Value);
        }
        public ColorComponentSearchResult ComputeResult(WeightedValue<HdrRGB>[] colors)
        {
            Contract.Requires(colors != null);
            Contract.Requires(colors.Length != 0);

            var result = new ColorComponentSearchResult();

            //colors = new[] { new WeightedValue<HdrRGB>(new HdrRGB(213, 198, 185), 0) };

            var directMixed = ColorMixer.Mix(FilteringPars, MixingPars, colors);
            result.DirectResult = new MixedColor(TargetColor, directMixed);

            //double minWeight = mixDict.Values.Select(c => c.Weight).Min();
            //double bestSumMod = double.MaxValue;
            //WeightedBaseColor[] bestMixes = null;
            //foreach (double prime in TestPrimes)
            //{
            //    var mixes = new WeightedBaseColor[mixDict.Count];
            //    double sumMod = 0.0;
            //    int mixIndex = 0;
            //    foreach (var mix in mixDict.Values)
            //    {
            //        double newWeight = (mix.Weight * prime) / minWeight;
            //        double roundedNewWeight = Math.Round(newWeight, 0, MidpointRounding.AwayFromZero);
            //        mixes[mixIndex++] = new WeightedBaseColor(mix.Value, roundedNewWeight);
            //        sumMod += Math.Abs(newWeight - roundedNewWeight);
            //    }
            //    if (sumMod < bestSumMod)
            //    {
            //        bestSumMod = sumMod;
            //        bestMixes = mixes;
            //    }
            //}

            //result.AdjusmentMod = bestSumMod;
            //result.MaxWeight = (int)bestMixes.Max(c => c.Weight);

            //colors = bestMixes.Select(c => new WeightedValue<HdrRGB>(c.Value.Color, c.Weight)).ToArray();
            
            //var finalMixed = ColorMixer.Mix(FilteringPars, MixingPars, colors);
            //result.FinalResult = new MixedColor(TargetColor, finalMixed);

            result.FinalResult = result.DirectResult;

            return result;
        }
Example #10
0
    private ReactiveCollection <WeightedValue> GenerateStarElements(int index)
    {
        int ElementCount = _elements.Count;

        float curve = index * (ElementCount * _starsConfig.MaxElementsBellCurveMagnifier / _starsConfig.Stars.Count);
        float ofset = index * (ElementCount / _starsConfig.Stars.Count);

        ReactiveCollection <WeightedValue> output = new ReactiveCollection <WeightedValue>();
        float probability;

        for (int i = 0; i < ElementCount; i++)
        {
            probability = (1 / Mathf.Sqrt(2 * Mathf.PI * curve)) * Mathf.Exp(-Mathf.Pow(ofset - i, 2) / (2 * curve));
            if (probability >= .01)
            {
                WeightedValue element = new WeightedValue(i + 1, probability);
                output.Add(element);
            }
        }

        return(output);
    }
Example #11
0
 private static double ComputeAdditiveComponent(WeightedValue<Color>[] colors, double weightSum, Func<Color, byte> selectComponent)
 {
     return ToByteRange(Add(colors.Select(c => new WeightedValue<double>(selectComponent(c.Value), c.Weight / weightSum))));
 }
Example #12
0
 private static double ComputeAvgComponent(WeightedValue<Color>[] colors, double weightSum, Func<Color, byte> selectComponent)
 {
     return ToByteRange(colors.Select(c => selectComponent(c.Value) * ((c.Weight / weightSum) * 2.0)).Average());
 }
Example #13
0
        /// <summary>
        /// Precalculates weighted values for each index of point in new line (Box filter).
        /// </summary>
        /// <param name="oldLength">The old line length.</param>
        /// <param name="newLength">The new line length.</param>
        /// <param name="startFrom">Starting index for weights values.</param>
        /// <param name="prepareCount">Count of weights values to prepare.</param>
        private void PrepareWeightsBox(int oldLength, int newLength, int startFrom = 0, int prepareCount = 0)
        {
            Debug.Assert(startFrom >= 0, "startFrom should be no less than 0.");
            Debug.Assert(startFrom + prepareCount <= newLength, "prepareCount should not go over newLength.");

            if (prepareCount <= 0)
            {
                prepareCount = newLength - startFrom;
            }

            double scale  = (double)newLength / (double)oldLength;
            double radius = scale < 1.0 ? radiusSmoothing / scale : radiusSmoothing;

            weightedValues = new WeightedValue[prepareCount][];

            // By all indexes in new line
            Parallel.For(startFrom, startFrom + prepareCount,
                         i =>
            {
                double center = i / scale;
                int left;
                int right;
                if (scale > 1.0)
                {
                    left = right = (int)(center + 0.5);
                }
                else
                {
                    left  = (int)Math.Floor(center - radius);
                    right = (int)Math.Ceiling(center + radius);
                }

                // Add correction for Box filter (radiusSmoothing == 0.5) and calculate weight of element
                int correctionSize = right - left + 1;
                int weight         = (correctionSize == 1) ? Bias : (Bias + correctionSize / 2) / correctionSize;

                weightedValues[i - startFrom] = new WeightedValue[correctionSize];
                for (int j = left, count = 0; j <= right; j++, count++)
                {
                    // Calculate index of pixel in old array
                    int index;
                    if (j < 0)
                    {
                        index = -j;
                    }
                    else if (j >= oldLength)
                    {
                        index = oldLength * 2 - j - 1;
                    }
                    else
                    {
                        index = j;
                    }

                    // Save weighted value
                    weightedValues[i - startFrom][count] = new WeightedValue {
                        Index = index, Weight = weight
                    };
                }
            });
        }
Example #14
0
        /// <summary>
        /// Precalculates weighted values for each index of point in new line.
        /// </summary>
        /// <param name="oldLength">The old line length.</param>
        /// <param name="newLength">The new line length.</param>
        /// <param name="startFrom">Starting index for weights values.</param>
        /// <param name="prepareCount">Count of weights values to prepare.</param>
        private void PrepareWeights(int oldLength, int newLength, int startFrom = 0, int prepareCount = 0)
        {
            Debug.Assert(startFrom >= 0, "startFrom should be no less than 0.");
            Debug.Assert(startFrom + prepareCount <= newLength, "prepareCount should not go over newLength.");

            if (prepareCount <= 0)
            {
                prepareCount = newLength - startFrom;
            }

            double scale = (double)newLength / (double)oldLength;
            double fscale;
            double radius;

            if (scale < 1.0)
            {
                radius = radiusSmoothing / scale;
                fscale = 1.0 / scale;
            }
            else
            {
                radius = radiusSmoothing;
                fscale = 1.0;
            }

            weightedValues = new WeightedValue[prepareCount][];

            // By all indexes in new line
            Parallel.For(startFrom, startFrom + prepareCount,
                         i =>
            {
                double center = i / scale;
                int left      = (int)Math.Floor(center - radius);
                int right     = (int)Math.Ceiling(center + radius);

                weightedValues[i - startFrom] = new WeightedValue[right - left + 1];
                int count = 0;

                for (int j = left; j <= right; j++)
                {
                    // Calculate weight of element
                    int weight = (int)(filterFunc((center - j) / fscale) / fscale * Bias + 0.5);

                    if (weight != 0)
                    {
                        // Calculate index of pixel in old array
                        int index;
                        if (j < 0)
                        {
                            index = -j;
                        }
                        else if (j >= oldLength)
                        {
                            index = oldLength * 2 - j - 1;
                        }
                        else
                        {
                            index = j;
                        }

                        // Save weighted value
                        weightedValues[i - startFrom][count++] = new WeightedValue {
                            Index = index, Weight = weight
                        };
                    }
                }

                Array.Resize(ref weightedValues[i - startFrom], count);
            });
        }
Example #15
0
 public void RemoveOption(WeightedValue value)
 {
     options.Remove(value);
 }
Example #16
0
 static IEnumerable <WeightedValue> ExcludeValue(IEnumerable <WeightedValue> set, WeightedValue value)
 {
     foreach (var setValue in set)
     {
         if (setValue == value)
         {
             continue;
         }
         else
         {
             yield return(setValue);
         }
     }
 }