Skip to content

Commit

Permalink
v2.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
msasanmh committed Oct 16, 2023
1 parent 3c28684 commit bcfde15
Show file tree
Hide file tree
Showing 163 changed files with 34,210 additions and 24,536 deletions.
47 changes: 47 additions & 0 deletions MsmhToolsClass/MsmhToolsClass/ByteArrayTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace MsmhToolsClass;

public class ByteArrayTool
{
public static int Search(byte[] src, byte[] pattern)
{
int maxFirstCharSlot = src.Length - pattern.Length + 1;
for (int i = 0; i < maxFirstCharSlot; i++)
{
if (src[i] != pattern[0]) // compare only first byte
continue;

// found a match on first byte, now try to match rest of the pattern
for (int j = pattern.Length - 1; j >= 1; j--)
{
if (src[i + j] != pattern[j]) break;
if (j == 1) return i;
}
}
return -1;
}

/// <summary>
/// Fully read a stream into a byte array.
/// </summary>
/// <param name="input">The input stream.</param>
/// <returns>A byte array containing the data read from the stream.</returns>
public static byte[] StreamToBytes(Stream input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
if (!input.CanRead) throw new InvalidOperationException("Input stream is not readable");

byte[] buffer = new byte[16 * 1024];
using MemoryStream ms = new();
int read;

while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}

return ms.ToArray();
}

}
358 changes: 180 additions & 178 deletions MsmhToolsClass/MsmhToolsClass/CertificateTool.cs

Large diffs are not rendered by default.

153 changes: 73 additions & 80 deletions MsmhToolsClass/MsmhToolsClass/ColorsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,86 @@
using System.Drawing;

#nullable enable
namespace MsmhToolsClass
namespace MsmhToolsClass;

public static class ColorsTool
{
public static class ColorsTool
//-----------------------------------------------------------------------------------
/// <summary>
/// Converts the HSL values to a Color.
/// </summary>
/// <param name="alpha">The alpha. (0 - 255)</param>
/// <param name="hue">The hue. (0f - 360f)</param>
/// <param name="saturation">The saturation. (0f - 1f)</param>
/// <param name="lighting">The lighting. (0f - 1f)</param>
/// <returns></returns>
public static Color FromHsl(int alpha, float hue, float saturation, float lighting)
{
//-----------------------------------------------------------------------------------
/// <summary>
/// Converts the HSL values to a Color.
/// </summary>
/// <param name="alpha">The alpha. (0 - 255)</param>
/// <param name="hue">The hue. (0f - 360f)</param>
/// <param name="saturation">The saturation. (0f - 1f)</param>
/// <param name="lighting">The lighting. (0f - 1f)</param>
/// <returns></returns>
public static Color FromHsl(int alpha, float hue, float saturation, float lighting)
if (0 > alpha || 255 < alpha)
{
throw new ArgumentOutOfRangeException(nameof(alpha));
}
if (0f > hue || 360f < hue)
{
if (0 > alpha || 255 < alpha)
{
throw new ArgumentOutOfRangeException(nameof(alpha));
}
if (0f > hue || 360f < hue)
{
throw new ArgumentOutOfRangeException(nameof(hue));
}
if (0f > saturation || 1f < saturation)
{
throw new ArgumentOutOfRangeException(nameof(saturation));
}
if (0f > lighting || 1f < lighting)
{
throw new ArgumentOutOfRangeException(nameof(lighting));
}
throw new ArgumentOutOfRangeException(nameof(hue));
}
if (0f > saturation || 1f < saturation)
{
throw new ArgumentOutOfRangeException(nameof(saturation));
}
if (0f > lighting || 1f < lighting)
{
throw new ArgumentOutOfRangeException(nameof(lighting));
}

if (0 == saturation)
{
return Color.FromArgb(alpha, Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255));
}
if (0 == saturation)
{
return Color.FromArgb(alpha, Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255));
}

float fMax, fMid, fMin;
int iSextant, iMax, iMid, iMin;
float fMax, fMid, fMin;
int iSextant, iMax, iMid, iMin;

if (0.5 < lighting)
{
fMax = lighting - (lighting * saturation) + saturation;
fMin = lighting + (lighting * saturation) - saturation;
}
else
{
fMax = lighting + (lighting * saturation);
fMin = lighting - (lighting * saturation);
}
if (0.5 < lighting)
{
fMax = lighting - (lighting * saturation) + saturation;
fMin = lighting + (lighting * saturation) - saturation;
}
else
{
fMax = lighting + (lighting * saturation);
fMin = lighting - (lighting * saturation);
}

iSextant = (int)Math.Floor(hue / 60f);
if (300f <= hue)
{
hue -= 360f;
}
hue /= 60f;
hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
if (0 == iSextant % 2)
{
fMid = hue * (fMax - fMin) + fMin;
}
else
{
fMid = fMin - hue * (fMax - fMin);
}
iSextant = (int)Math.Floor(hue / 60f);
if (300f <= hue)
{
hue -= 360f;
}
hue /= 60f;
hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
if (0 == iSextant % 2)
{
fMid = hue * (fMax - fMin) + fMin;
}
else
{
fMid = fMin - hue * (fMax - fMin);
}

iMax = Convert.ToInt32(fMax * 255);
iMid = Convert.ToInt32(fMid * 255);
iMin = Convert.ToInt32(fMin * 255);
iMax = Convert.ToInt32(fMax * 255);
iMid = Convert.ToInt32(fMid * 255);
iMin = Convert.ToInt32(fMin * 255);

switch (iSextant)
{
case 1:
return Color.FromArgb(alpha, iMid, iMax, iMin);
case 2:
return Color.FromArgb(alpha, iMin, iMax, iMid);
case 3:
return Color.FromArgb(alpha, iMin, iMid, iMax);
case 4:
return Color.FromArgb(alpha, iMid, iMin, iMax);
case 5:
return Color.FromArgb(alpha, iMax, iMin, iMid);
default:
return Color.FromArgb(alpha, iMax, iMid, iMin);
}
}
//-----------------------------------------------------------------------------------
return iSextant switch
{
1 => Color.FromArgb(alpha, iMid, iMax, iMin),
2 => Color.FromArgb(alpha, iMin, iMax, iMid),
3 => Color.FromArgb(alpha, iMin, iMid, iMax),
4 => Color.FromArgb(alpha, iMid, iMin, iMax),
5 => Color.FromArgb(alpha, iMax, iMin, iMid),
_ => Color.FromArgb(alpha, iMax, iMid, iMin),
};
}
}
//-----------------------------------------------------------------------------------
}
127 changes: 80 additions & 47 deletions MsmhToolsClass/MsmhToolsClass/ConvertTool.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,98 @@
using System;
using System.Diagnostics;
using System.Diagnostics;

#nullable enable
namespace MsmhToolsClass
namespace MsmhToolsClass;

public class ConvertTool
{
public class ConvertTool
public static string TimeSpanToHumanRead(TimeSpan eTime, bool fullRead)
{
public enum SizeUnits
string days = eTime.Days.ToString().TrimStart('0').PadLeft(1, '0');
eTime = TimeSpan.FromMilliseconds(Math.Round(eTime.TotalMilliseconds, 2));
string hours = eTime.Hours.ToString().TrimStart('0').PadLeft(1, '0');
string minutes = eTime.Minutes.ToString().TrimStart('0').PadLeft(1, '0');
string seconds = eTime.Seconds.ToString().TrimStart('0').PadLeft(1, '0');
string milliseconds = eTime.Milliseconds.ToString().TrimStart('0').PadLeft(1, '0');

string result = string.Empty;
if (fullRead)
{
Byte, KB, MB, GB, TB, PB, EB, ZB, YB, RB, QB
if (eTime.Days > 0) result += eTime.Days > 1 ? $"{days} Days, " : $"{days} Day, ";
if (eTime.Hours > 0) result += eTime.Hours > 1 ? $"{hours} Hours, " : $"{hours} Hour, ";
if (eTime.Minutes > 0) result += eTime.Minutes > 1 ? $"{minutes} Minutes, " : $"{minutes} Minute, ";
if (eTime.Seconds > 0) result += eTime.Seconds > 1 ? $"{seconds} Seconds, " : $"{seconds} Second, ";
if (eTime.Milliseconds > 0) result += eTime.Milliseconds > 1 ? $"{milliseconds} Milliseconds" : $"{milliseconds} Millisecond";
result = result.Trim();
if (result.EndsWith(',')) result = result.TrimEnd(',');
}

public static string ConvertByteToHumanRead(double bytes)
else
{
string result = $"{bytes} {SizeUnits.Byte}";
double calc = bytes;
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.Byte, SizeUnits.KB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.KB, SizeUnits.MB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.MB, SizeUnits.GB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.GB, SizeUnits.TB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.TB, SizeUnits.PB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.PB, SizeUnits.EB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.EB, SizeUnits.ZB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.ZB, SizeUnits.YB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.YB, SizeUnits.RB, out result);
if (calc > 1000) ConvertSize(calc, SizeUnits.RB, SizeUnits.QB, out result);
return result;
if (eTime.Days > 0) result += $"{days}:";
if (eTime.Hours > 0) result += $"{hours}:";
if (eTime.Minutes > 0) result += $"{minutes}:";
if (eTime.Seconds > 0) result += $"{seconds}.";
if (eTime.Milliseconds > 0) result += $"{milliseconds}";
if (result.EndsWith(':')) result = result.TrimEnd(':');
if (result.EndsWith('.')) result = result.TrimEnd('.');
}

return result;
}

public static double ConvertSize(double value, SizeUnits fromUnit, SizeUnits toUnit, out string humanRead)
public enum SizeUnits
{
Byte, KB, MB, GB, TB, PB, EB, ZB, YB, RB, QB
}

public static string ConvertByteToHumanRead(double bytes)
{
bytes = Math.Round(bytes, 2);
string result = $"{bytes} {SizeUnits.Byte}";
double calc = bytes;
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.Byte, SizeUnits.KB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.KB, SizeUnits.MB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.MB, SizeUnits.GB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.GB, SizeUnits.TB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.TB, SizeUnits.PB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.PB, SizeUnits.EB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.EB, SizeUnits.ZB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.ZB, SizeUnits.YB, out result);
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.YB, SizeUnits.RB, out result);
if (calc > 1000) ConvertSize(calc, SizeUnits.RB, SizeUnits.QB, out result);
return result;
}

public static double ConvertSize(double value, SizeUnits fromUnit, SizeUnits toUnit, out string humanRead)
{
try
{
try
double unit = 1000; // In decimal it's 1000 not 1024. ref:https://en.wikipedia.org/wiki/Byte#Multiple-byte_units
double valueByte = value * (double)Math.Pow(unit, (long)fromUnit);
double outValue = valueByte / (double)Math.Pow(unit, (long)toUnit);
string outString = outValue.ToString();
if (outString.Contains('.') && outString.EndsWith('0'))
{
double unit = 1000; // In decimal it's 1000 not 1024. ref:https://en.wikipedia.org/wiki/Byte#Multiple-byte_units
double valueByte = value * (double)Math.Pow(unit, (long)fromUnit);
double outValue = valueByte / (double)Math.Pow(unit, (long)toUnit);
string outString = outValue.ToString();
if (outString.Contains('.') && outString.EndsWith('0'))
while (outString.EndsWith('0'))
{
while (outString.EndsWith('0'))
{
outString = outString.TrimEnd('0');
}
outString = outString.TrimEnd('.');
double result = double.TryParse(outString, out double outValueNoZero) ? outValueNoZero : outValue;
humanRead = $"{Math.Round(result, 2)} {toUnit}";
return result;
}
else
{
humanRead = $"{Math.Round(outValue, 2)} {toUnit}";
return outValue;
outString = outString.TrimEnd('0');
}
outString = outString.TrimEnd('.');
double result = double.TryParse(outString, out double outValueNoZero) ? outValueNoZero : outValue;
humanRead = $"{Math.Round(result, 2)} {toUnit}";
return result;
}
catch (Exception ex)
else
{
Debug.WriteLine($"ConvertSize: {ex.Message}");
humanRead = $"-1 {toUnit}";
return -1;
humanRead = $"{Math.Round(outValue, 2)} {toUnit}";
return outValue;
}
}
//-----------------------------------------------------------------------------------
catch (Exception ex)
{
Debug.WriteLine($"ConvertSize: {ex.Message}");
humanRead = $"-1 {toUnit}";
return -1;
}
}
}
//-----------------------------------------------------------------------------------
}
Loading

0 comments on commit bcfde15

Please sign in to comment.