Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SizeExtensions.cs « Extensions « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f170e12a7e87169872c5b7ae31fd85bf899783aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace UVtools.Core.Extensions
{
    public static class SizeExtensions
    {
        public static readonly string[] SizeSuffixes =
            { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };

        public static string SizeSuffix(long value, byte decimalPlaces = 2)
        {
            //if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
            if (value < 0) { return "-" + SizeSuffix(-value); }
            if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }

            // mag is 0 for bytes, 1 for KB, 2, for MB, etc.
            int mag = (int)Math.Log(value, 1024);

            // 1L << (mag * 10) == 2 ^ (10 * mag) 
            // [i.e. the number of bytes in the unit corresponding to mag]
            decimal adjustedSize = (decimal)value / (1L << (mag * 10));

            // make adjustment when the value is large enough that
            // it would round up to 1000 or more
            if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
            {
                mag += 1;
                adjustedSize /= 1024;
            }

            return string.Format("{0:n" + decimalPlaces + "} {1}",
                adjustedSize,
                SizeSuffixes[mag]);
        }

        public static Size Inflate(this Size size, int pixels) => new (size.Width + pixels, size.Height + pixels);
        public static Size Inflate(this Size size, int width, int height) => new (size.Width + width, size.Height + height);

        /// <summary>
        /// Exchange width with height
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Size Invert(this Size size) => new(size.Height, size.Width);

        public static int Area(this Rectangle rect)
        {
            return rect.Width * rect.Height;
        }

        public static int Area(this Size size)
        {
            return size.Width * size.Height;
        }

        public static int Max(this Size size)
        {
            return Math.Max(size.Width, size.Height);
        }


        public static float Area(this RectangleF rect, int round = -1)
        {
            return round >= 0 ? (float) Math.Round(rect.Width * rect.Height, round) : rect.Width * rect.Height;
        }

        public static float Area(this SizeF size, int round = -1)
        {
            return round >= 0 ? (float)Math.Round(size.Width * size.Height, round) : size.Width * size.Height;
        }

        public static float Max(this SizeF size)
        {
            return Math.Max(size.Width, size.Height);
        }

    }
}