tesses-profileicon/Tesses.ProfileIcon/Class1.cs

155 lines
5.6 KiB
C#

/*
Tesses.ProfileIcon A profile Icon Generator
Copyright (C) 2023 Mike Nolan
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 3 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, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Processing;
namespace Tesses.ProfileIcon
{
public class ProfileIconCreator
{
public ProfileIconCreator()
{
}
public ProfileIconCreator WithMonoChrome()
{
return WithColor(0,0,0,255,255,255).WithColor(255,255,255,0,0,0);
}
public ProfileIconCreator WithDefaultColors()
{
return WithMonoChrome()
.WithColor(0,0,0,210,16,230)
.WithColor(255,255,255,0,87,255)
.WithColor(255,255,255,0,255,0)
.WithColor(0,0,0,255,0,0)
.WithColor(0,0,0,255,255,0)
.WithColor(0,0,0,213,252,0)
.WithColor(0,0,0,255,181,243)
.WithColor(0,0,0,255,181,181)
.WithColor(255,255,255,154,14,14)
.WithColor(255,255,255,14,134,153)
.WithColor(255,255,255,222,133,0)
.WithColor(255,255,255,0,164,0);
}
public ProfileIconCreator WithColor(byte fr,byte fg,byte fb,byte br,byte bg,byte bb)
{
return WithColor(new Rgb24(fr,fg,fb),new Rgb24(br,bg,bb));
}
public ProfileIconCreator WithColor(Rgb24 foreground,Rgb24 background)
{
Colors.Add((foreground,background));
return this;
}
public ProfileIconCreator WithFont(Font font)
{
this.Font = font;
return this;
}
public ProfileIconCreator WithDefaultFont(float em=360)
{
FontFamily fam;
FontCollection collection=new FontCollection();
using(var strm = GetType().Assembly.GetManifestResourceStream("Tesses.ProfileIcon.assets.FreeMonoBold.ttf"))
fam=collection.Add(strm);
Font font = new Font(fam,em);
return WithFont(font);
}
public ProfileIconCreator WithSize(Size size)
{
return this;
}
public ProfileIconCreator WithProfileIconColorPicker(ProfileIconColorPicker picker)
{
this.Picker = picker;
return this;
}
public ProfileIconCreator WithHashedProfileIconColorPicker()
{
return WithProfileIconColorPicker(new HashedProfileIconColorPicker());
}
public ProfileIconCreator WithProfileIconColorPicker(ProfileIconColorPickerDelegate del)
{
return this.WithProfileIconColorPicker(ProfileIconColorPicker.FromDelegate(del));
}
public Image<Rgb24> GenerateCustomText(string text,Rgb24 foreground,Rgb24 background,Font font,Size size)
{
Image<Rgb24> image = new Image<Rgb24>(size.Width,size.Height,background);
var res=TextMeasurer.MeasureSize(text,new TextOptions(font));
float x = ((float)size.Width/2) - ((res.Width-res.X)/2);
float y = ((float)size.Height/2) - ((res.Height-res.Y)/2);
image.Mutate(e=>{
e.DrawText(text,font,foreground,new PointF(x,y));
});
return image;
}
public Image<Rgb24> GenerateCustomText(string text,Rgb24 foreground,Rgb24 background)
{
if(Font == null)WithDefaultFont();
return GenerateCustomText(text,foreground,background,Font,Size);
}
public Image<Rgb24> GenerateCustomText(string text)
{
if(Colors.Count == 0) WithDefaultColors();
int myColor = Picker.GetColor(this,text) % Colors.Count;
var (foreground,background) = Colors[myColor];
return GenerateCustomText(text,foreground,background);
}
public Image<Rgb24> Generate(string name)
{
int myColor = Picker.GetColor(this,name) % Colors.Count;
var (foreground,background) = Colors[myColor];
string[] nameParts = name.Split(' ');
StringBuilder b= new StringBuilder();
foreach(var nP in nameParts)
{
if(nP.Length > 0)
b.Append(nP[0]);
}
string nameP=b.ToString().ToUpper();
return GenerateCustomText(nameP,foreground,background);
}
public Image<Rgb24> Generate(string text,bool customText)
{
if(customText) return GenerateCustomText(text);
return Generate(text);
}
public List<(Rgb24 Foreground, Rgb24 Background)> Colors {get;set;}=new List<(Rgb24 Foreground, Rgb24 Background)>();
public ProfileIconColorPicker Picker {get;set;}=ProfileIconColorPicker.FromDelegate((a,b)=>2);
public Size Size {get;set;}=new Size(900);
public Font Font {get;set;}=null;
}
}