2014-08-11 22:40:14 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2014-08-11 22:40:14 +12:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
2016-07-30 05:32:01 +12:00
|
|
|
|
using System;
|
2020-09-26 17:11:12 +12:00
|
|
|
|
using System.Collections.Generic;
|
2016-07-30 05:32:01 +12:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
2016-07-29 13:31:46 +12:00
|
|
|
|
public class CodeMenuEntryPixelInfo : CodeMenuEntry
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
2020-09-26 23:31:56 +12:00
|
|
|
|
protected override string Prefix { get; } = "$";
|
2020-09-26 17:11:12 +12:00
|
|
|
|
|
|
|
|
|
// This shouldn't show up in the list of options, but will continue to work for backwards compatibility's sake.
|
2020-09-26 23:31:56 +12:00
|
|
|
|
private static readonly CodeMenuEntryPixelInfo r = new CodeMenuEntryPixelInfo("r", "Red color (0-255)");
|
|
|
|
|
private static readonly CodeMenuEntryPixelInfo g = new CodeMenuEntryPixelInfo("g", "Green color (0-255)");
|
|
|
|
|
private static readonly CodeMenuEntryPixelInfo b = new CodeMenuEntryPixelInfo("b", "Blue color (0-255)");
|
2020-09-26 17:11:12 +12:00
|
|
|
|
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo r255 = new CodeMenuEntryPixelInfo("r255", "Red color (0-255)");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo g255 = new CodeMenuEntryPixelInfo("g255", "Green color (0-255)");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo b255 = new CodeMenuEntryPixelInfo("b255", "Blue color (0-255)");
|
2020-09-26 23:31:56 +12:00
|
|
|
|
public static readonly CodeMenuEntryPixelInfo r1 = new CodeMenuEntryPixelInfo("r1", "Red color (0-1). Specify decimal precision with {n}, defaults to 3.");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo g1 = new CodeMenuEntryPixelInfo("g1", "Green color (0-1). Specify decimal precision with {n}, defaults to 3.");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo b1 = new CodeMenuEntryPixelInfo("b1", "Blue color (0-1). Specify decimal precision with {n}, defaults to 3.");
|
2016-07-30 05:32:01 +12:00
|
|
|
|
public static readonly CodeMenuEntryPixelInfo hex = new CodeMenuEntryPixelInfo("hex", "Hex color value (Lowercase)");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo HEX = new CodeMenuEntryPixelInfo("HEX", "Hex color value (Uppercase)");
|
2020-11-22 10:58:31 +13:00
|
|
|
|
public static readonly CodeMenuEntryPixelInfo c100 = new CodeMenuEntryPixelInfo("c100", "Cyan color (0-100)");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo m100 = new CodeMenuEntryPixelInfo("m100", "Magenta color (0-100)");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo y100 = new CodeMenuEntryPixelInfo("y100", "Yellow color (0-100)");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo k100 = new CodeMenuEntryPixelInfo("k100", "Key color (0-100)");
|
2020-10-01 11:46:04 +13:00
|
|
|
|
public static readonly CodeMenuEntryPixelInfo name = new CodeMenuEntryPixelInfo("name", "Color name");
|
2016-07-30 05:32:01 +12:00
|
|
|
|
public static readonly CodeMenuEntryPixelInfo x = new CodeMenuEntryPixelInfo("x", "X position");
|
|
|
|
|
public static readonly CodeMenuEntryPixelInfo y = new CodeMenuEntryPixelInfo("y", "Y position");
|
2016-07-29 13:31:46 +12:00
|
|
|
|
public static readonly CodeMenuEntryPixelInfo n = new CodeMenuEntryPixelInfo("n", "New line");
|
|
|
|
|
|
|
|
|
|
public CodeMenuEntryPixelInfo(string value, string description) : base(value, description)
|
2014-08-28 12:53:38 +12:00
|
|
|
|
{
|
|
|
|
|
}
|
2016-07-30 05:32:01 +12:00
|
|
|
|
|
|
|
|
|
public static string Parse(string input, Color color, Point position)
|
|
|
|
|
{
|
2020-09-26 17:11:12 +12:00
|
|
|
|
input = input.Replace(r255.ToPrefixString(), color.R.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(g255.ToPrefixString(), color.G.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(b255.ToPrefixString(), color.B.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
2016-07-30 05:32:01 +12:00
|
|
|
|
Replace(HEX.ToPrefixString(), ColorHelpers.ColorToHex(color), StringComparison.InvariantCulture).
|
|
|
|
|
Replace(hex.ToPrefixString(), ColorHelpers.ColorToHex(color).ToLowerInvariant(), StringComparison.InvariantCultureIgnoreCase).
|
2020-11-22 10:58:31 +13:00
|
|
|
|
Replace(c100.ToPrefixString(), Math.Round(ColorHelpers.ColorToCMYK(color).Cyan100, 2, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(m100.ToPrefixString(), Math.Round(ColorHelpers.ColorToCMYK(color).Magenta100, 2, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(y100.ToPrefixString(), Math.Round(ColorHelpers.ColorToCMYK(color).Yellow100, 2, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(k100.ToPrefixString(), Math.Round(ColorHelpers.ColorToCMYK(color).Key100, 2, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase).
|
2020-10-01 11:46:04 +13:00
|
|
|
|
Replace(name.ToPrefixString(), ColorHelpers.GetColorName(color), StringComparison.InvariantCultureIgnoreCase).
|
2016-07-30 05:32:01 +12:00
|
|
|
|
Replace(x.ToPrefixString(), position.X.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(y.ToPrefixString(), position.Y.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(n.ToPrefixString(), Environment.NewLine, StringComparison.InvariantCultureIgnoreCase);
|
2020-09-26 23:31:56 +12:00
|
|
|
|
|
2020-09-26 17:11:12 +12:00
|
|
|
|
foreach (Tuple<string, int> entry in ListEntryWithValue(input, r1.ToPrefixString()))
|
|
|
|
|
{
|
2020-09-26 23:31:56 +12:00
|
|
|
|
input = input.Replace(entry.Item1, Math.Round(color.R / 255d, MathHelpers.Clamp(entry.Item2, 0, 15), MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase);
|
2020-09-26 17:11:12 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Tuple<string, int> entry in ListEntryWithValue(input, g1.ToPrefixString()))
|
|
|
|
|
{
|
2020-09-26 23:31:56 +12:00
|
|
|
|
input = input.Replace(entry.Item1, Math.Round(color.G / 255d, MathHelpers.Clamp(entry.Item2, 0, 15), MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase);
|
2020-09-26 17:11:12 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Tuple<string, int> entry in ListEntryWithValue(input, b1.ToPrefixString()))
|
|
|
|
|
{
|
2020-09-26 23:31:56 +12:00
|
|
|
|
input = input.Replace(entry.Item1, Math.Round(color.B / 255d, MathHelpers.Clamp(entry.Item2, 0, 15), MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase);
|
2020-09-26 17:11:12 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 23:31:56 +12:00
|
|
|
|
input = input.Replace(r1.ToPrefixString(), Math.Round(color.R / 255d, 3, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(g1.ToPrefixString(), Math.Round(color.G / 255d, 3, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(b1.ToPrefixString(), Math.Round(color.B / 255d, 3, MidpointRounding.AwayFromZero).ToString(), StringComparison.InvariantCultureIgnoreCase);
|
2020-09-26 17:11:12 +12:00
|
|
|
|
|
2020-09-26 23:31:56 +12:00
|
|
|
|
input = input.Replace(r.ToPrefixString(), color.R.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(g.ToPrefixString(), color.G.ToString(), StringComparison.InvariantCultureIgnoreCase).
|
|
|
|
|
Replace(b.ToPrefixString(), color.B.ToString(), StringComparison.InvariantCultureIgnoreCase);
|
2020-09-26 17:11:12 +12:00
|
|
|
|
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Tuple<string, string[]>> ListEntryWithArguments(string text, string entry, int elements)
|
|
|
|
|
{
|
|
|
|
|
foreach (Tuple<string, string> o in text.ForEachBetween(entry + "{", "}"))
|
|
|
|
|
{
|
|
|
|
|
string[] s = o.Item2.Split(',');
|
|
|
|
|
if (elements > s.Length)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref s, elements);
|
|
|
|
|
}
|
|
|
|
|
yield return new Tuple<string, string[]>(o.Item1, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Tuple<string, int[]>> ListEntryWithValues(string text, string entry, int elements)
|
|
|
|
|
{
|
|
|
|
|
foreach (Tuple<string, string[]> o in ListEntryWithArguments(text, entry, elements))
|
|
|
|
|
{
|
|
|
|
|
int[] a = new int[o.Item2.Length];
|
|
|
|
|
for (int i = o.Item2.Length - 1; i >= 0; --i)
|
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
if (int.TryParse(o.Item2[i], out int n))
|
2020-09-26 17:11:12 +12:00
|
|
|
|
{
|
|
|
|
|
a[i] = n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
yield return new Tuple<string, int[]>(o.Item1, a);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Tuple<string, int>> ListEntryWithValue(string text, string entry)
|
|
|
|
|
{
|
|
|
|
|
foreach (Tuple<string, int[]> o in ListEntryWithValues(text, entry, 1))
|
|
|
|
|
{
|
|
|
|
|
yield return new Tuple<string, int>(o.Item1, o.Item2[0]);
|
|
|
|
|
}
|
2016-07-30 05:32:01 +12:00
|
|
|
|
}
|
2014-08-11 03:36:08 +12:00
|
|
|
|
}
|
2014-08-11 22:40:14 +12:00
|
|
|
|
}
|