/// <summary>
        /// Gibt zurück, wie groß ein prozentuale Anteil der übergebenen Zahl ist.
        /// </summary>
        /// <param name="i">Die Zahl, die als 100% angenommen wird.</param>
        /// <param name="percentage">Der prozentuale Anteil</param>
        /// <param name="type">Gibt an, wie <paramref name="percentage"/> zu interpretieren ist.</param>
        /// <returns>Der prozentuale Anteil</returns>
        public static float GetPercentageAmount(this int i, float percentage, PercentageType type = PercentageType.Arithmetic)
        {
            if (i < 0 || percentage < 0)
            {
                throw new ArgumentException("Calculation of percentage is not supported for negative values");
            }

            return(type == PercentageType.Arithmetic ? i * percentage : i *(percentage / 100));
        }
        /// <summary>
        /// Gibt an, wie viel Prozent die übergebene Zahl in Relation zur Gesamtheit darstellt.
        /// </summary>
        /// <param name="i">Die Zahl, die zu prüfen ist.</param>
        /// <param name="everything">Die gesamte Menge</param>
        /// <param name="type">Der Typ in dem das Ergebnis zurückgegeben wird.</param>
        /// <returns>Der prozentuale Anteil der Zahl an der Gesamtheit.</returns>
        /// <exception cref="ArgumentException"><paramref name="i"/> oder <paramref name="everything"/> sind kleiner als 0.</exception>
        public static float AsPercentageOf(this long i, long everything, PercentageType type = PercentageType.Arithmetic)
        {
            if (i < 0 || everything < 0)
            {
                throw new ArgumentException("Calculation of percentage is not supported for negative values");
            }

            if (everything == 0)
            {
                throw new ArgumentException("The total amount must be greater 0.");
            }

            return(type == PercentageType.Arithmetic ? i * 1.0f / everything : i * 100f / everything);
        }
        private State GetDriveState(DriveInformation driveInformation, PercentageType percentageType, int errorPercentage)
        {
            switch (percentageType)
            {
            case PercentageType.PercentageUsed:
                return(driveInformation.UsedPercentage >= errorPercentage ? State.Failed : State.Ok);

            case PercentageType.PercentageRemaining:
                return(driveInformation.AvailablePercentage <= errorPercentage ? State.Failed : State.Ok);

            default:
                throw new NotImplementedException($"Percentage type \"{percentageType}\" is not supported.");
            }
        }
        /// <summary>
        /// Gibt an, wie viel Prozent die übergebene Zahl in Relation zur Gesamtheit darstellt.
        /// </summary>
        /// <param name="i">Die Zahl, die zu prüfen ist.</param>
        /// <param name="everything">Die gesamte Menge</param>
        /// <param name="type">Der Typ in dem das Ergebnis zurückgegeben wird.</param>
        /// <returns>Der prozentuale Anteil der Zahl an der Gesamtheit.</returns>
        /// <exception cref="ArgumentException"><paramref name="i"/> oder <paramref name="everything"/> sind kleiner als 0.</exception>
        public static double AsPercentageOf(this double i, double everything, PercentageType type = PercentageType.Arithmetic)
        {
            if (i < 0 || everything < 0)
            {
                throw new ArgumentException("Calculation of percentage is not supported for negative values");
            }

            if (everything.Equals(0d))
            {
                throw new ArgumentException("The total amount must be greater 0.");
            }

            return(type == PercentageType.Arithmetic ? i / everything : i * 100 / everything);
        }
        private int GetDrivePercentage(DriveInformation driveInformation, PercentageType percentageType)
        {
            switch (percentageType)
            {
            case PercentageType.PercentageUsed:
                return(driveInformation.UsedPercentage);

            case PercentageType.PercentageRemaining:
                return(driveInformation.AvailablePercentage);

            default:
                throw new NotImplementedException($"Percentage type \"{percentageType}\" is not supported");
            }
        }
        private string GetDriveMessage(DriveInformation driveInformation, PercentageType percentageType)
        {
            switch (percentageType)
            {
            case PercentageType.PercentageUsed:
                return($"{driveInformation.Drive} - {driveInformation.UsedPercentage}%{Environment.NewLine}" +
                       $"{driveInformation.TotalNumberOfUsedGigabytes} GB used out of {driveInformation.TotalNumberOfGigabytes} GB");

            case PercentageType.PercentageRemaining:
                return($"{driveInformation.Drive} - {driveInformation.AvailablePercentage}%{Environment.NewLine}" +
                       $"{driveInformation.TotalNumberOfFreeGigabytes} GB available out of {driveInformation.TotalNumberOfGigabytes} GB");

            default:
                throw new NotImplementedException($"Percentage type \"{percentageType}\" is not supported.");
            }
        }
Exemple #7
0
        public static String PercentageString(PercentageType type, Int32 Current, Int32 Maximum)
        {
            switch (type)
            {
            case PercentageType.Begin:
                return("Conversion has begun");

            case PercentageType.Total:
                return($"Chapter {Current.ToString("D3")}: {GetPercentage(Current, Maximum):P}");

            case PercentageType.Partial:
                return($"Task {Current.ToString("D2")}: {GetPercentage(Current, Maximum):P}");

            case PercentageType.End:
                return($"Successfully converted all chapters {GetPercentage(Current, Maximum):P}.");

            case PercentageType.None:
                return("No chapters need converting.");

            default:
                return("Why the f**k do I need profanity in my code Eli?");
            }
        }
        public override Image ProcessImage(Image image)
        {
            PercentageType pType;
            Bitmap         bitmap;

            if (!PercentageType.TryParse(Type, true, out pType))
            {
                pType = PercentageType.Bar;
            }

            if (pType == PercentageType.Bar)
            {
                bitmap = new Bitmap(500, 50);
                using (Graphics objGraphics = Graphics.FromImage(bitmap))
                {
                    // Initialize graphics
                    objGraphics.Clear(Color.White);
                    objGraphics.SmoothingMode     = SmoothingMode.AntiAlias;
                    objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                    Pen borderPen = new Pen(Color.DarkGray, 6f);


                    Brush     colorBrush   = new SolidBrush(Color);
                    Rectangle barRectangle = new Rectangle(3, 3, Percentage * 5 - 6, 44);
                    objGraphics.FillRectangle(colorBrush, barRectangle);

                    Rectangle borderRectangle = new Rectangle(3, 3, 494, 44);
                    objGraphics.DrawRectangle(borderPen, borderRectangle);

                    // Draw text on image
                    // Use rectangle for text and align text to center of rectangle
                    var          font         = new Font("Arial", 34, FontStyle.Bold);
                    StringFormat stringFormat = new StringFormat();
                    stringFormat.Alignment     = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    Brush textBrush = new SolidBrush(MostDifferent(Color));
                    objGraphics.DrawString(Percentage + "%", font, textBrush, borderRectangle, stringFormat);

                    // Save indicator to file
                    objGraphics.Flush();
                }
            }
            else
            {
                bitmap = new Bitmap(1000, 1000);
                using (Graphics objGraphics = Graphics.FromImage(bitmap))
                {
                    // Initialize graphics
                    objGraphics.Clear(Color.White);
                    objGraphics.SmoothingMode     = SmoothingMode.AntiAlias;
                    objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                    Pen borderPen = new Pen(Color.DarkGray, 10f);

                    // Fill pie
                    // Degrees are taken clockwise, 0 is parallel with x
                    // For sweep angle we must convert percent to degrees (90/25 = 18/5)
                    float startAngle = -90.0F;
                    float sweepAngle = (18.0F / 5) * Percentage;

                    Rectangle rectangle  = new Rectangle(50, 50, 900, 900);
                    Brush     colorBrush = new SolidBrush(Color);
                    objGraphics.FillPie(colorBrush, rectangle, startAngle, sweepAngle);

                    // Fill inner circle with white
                    rectangle = new Rectangle(200, 200, 600, 600);
                    objGraphics.FillEllipse(Brushes.White, rectangle);

                    // Draw circles
                    rectangle = new Rectangle(50, 50, 900, 900);
                    objGraphics.DrawEllipse(borderPen, rectangle);
                    rectangle = new Rectangle(200, 200, 600, 600);
                    objGraphics.DrawEllipse(borderPen, rectangle);

                    // Draw text on image
                    // Use rectangle for text and align text to center of rectangle
                    var          font         = new Font("Arial", 130, FontStyle.Bold);
                    StringFormat stringFormat = new StringFormat();
                    stringFormat.Alignment     = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;

                    rectangle = new Rectangle(200, 400, 620, 200);
                    objGraphics.DrawString(Percentage + "%", font, Brushes.DarkGray, rectangle, stringFormat);

                    // Save indicator to file
                    objGraphics.Flush();
                }
            }
            return((Image)bitmap);
        }
Exemple #9
0
 protected void AddPercentage(PercentageType pt)
 {
     if (pt == PercentageType.PageLoaded)
         Percentage += 10;
     else
         Percentage += 1;
 }
Exemple #10
0
        public void AsPercentageOf_IntCases_ReturnsExpectedResult(int i, int reference, PercentageType type, float expectedResult)
        {
            // act
            var result = i.AsPercentageOf(reference, type);

            // assert
            result.Should().Be(expectedResult);
        }
Exemple #11
0
        public void GetPercentageAmount_ShortCases_ReturnsExpectedResult(short i, float percentage, PercentageType type, float expectedResult)
        {
            // act
            var result = i.GetPercentageAmount(percentage, type);

            // assert
            result.Should().Be(expectedResult);
        }
Exemple #12
0
        public void GetPercentageAmount_DecimalCases_ReturnsExpectedResult(decimal i, decimal percentage, PercentageType type, decimal expectedResult)
        {
            // act
            var result = i.GetPercentageAmount(percentage, type);

            // assert
            result.Should().Be(expectedResult);
        }
Exemple #13
0
        public void AsPercentageOf_DecimalCases_ReturnsExpectedResult(decimal i, decimal reference, PercentageType type, decimal expectedResult)
        {
            // act
            var result = i.AsPercentageOf(reference, type);

            // assert
            result.Should().Be(expectedResult);
        }
Exemple #14
0
        public void GetPercentageAmount_DoubleCases_ReturnsExpectedResult(double i, double percentage, PercentageType type, double expectedResult)
        {
            // act
            var result = i.GetPercentageAmount(percentage, type);

            // assert
            result.Should().Be(expectedResult);
        }
Exemple #15
0
        public void AsPercentageOf_DoubleCases_ReturnsExpectedResult(double i, double reference, PercentageType type, double expectedResult)
        {
            // act
            var result = i.AsPercentageOf(reference, type);

            // assert
            result.Should().Be(expectedResult);
        }