Files
UVtools/UVtools.Core/Extensions/SizeExtensions.cs
T
Tiago Conceição 5fc722e203 v2.6.0
* (Add) File format: Zcode (Uniz IBEE)
* (Add) PrusaSlicer Printer: Uniz IBEE
* (Add) Extract: Output an "Layer.ini" file containing per layer data and include the Configuration.ini for Zip file formats
* (Improvement) Zip: Increase the GCode parsing performance
* (Fix) File formats: Wasn't set bottom / light off delay per individual layer on generic formats, defaulting every layer to 0
* (Fix) Edit print paramenters: When changing bottom layer count, layers didnt update thier properties
2021-03-02 01:42:53 +00:00

81 lines
2.8 KiB
C#

/*
* 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);
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);
}
}
}