Переделал клас с vb.net на C#
Class на [C#]
Class на [C#]
Class на [vb.net]using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Color_Text
{
class MyCodeGradient
{
public MyCodeGradient(string text)
{
_text = text;
}
private readonly string _text;
public Color StartColor { get; set; }
public Color EndColor { get; set; }
public string Generate()
{
char[] chars = _text.ToCharArray();
List<Color> colors = BuildGradient(chars.Count(c => !c.Equals(' ')));
StringBuilder sb = new StringBuilder();
int compensator = 0;
for (int i = 0; i <= chars.Count() - 1; i++)
{
if (!chars.Equals(' '))
{
sb.Append(string.Format("[color={0}]{1}[/color]", ColorTranslator.ToHtml(colors[i - compensator]), chars));
}
else
{
sb.Append(' ');
compensator += 1;
}
}
return sb.ToString();
}
private List<Color> BuildGradient(int stepCount)
{
int rMin = EndColor.R;
int rMax = StartColor.R;
int gMin = EndColor.G;
int gMax = StartColor.G;
int bMin = EndColor.B;
int bMax = StartColor.B;
List<Color> colors = new List<Color>();
for (int i = 0; i <= stepCount - 1; i++)
{
int rAvg = rMax + Convert.ToInt32((rMin - rMax) * i / stepCount);
int gAvg = gMax + Convert.ToInt32((gMin - gMax) * i / stepCount);
int bAvg = bMax + Convert.ToInt32((bMin - bMax) * i / stepCount);
colors.Add(Color.FromArgb(rAvg, gAvg, bAvg));
}
return colors;
}
}
}
Option Strict On
Imports System.Text
Public Class MyCodeGradient
Public Sub New(text As String)
_text = text
End Sub
Private ReadOnly _text As String
Public Property StartColor As Color
Public Property EndColor As Color
Public Function Generate() As String
Dim chars As Char() = _text.ToCharArray()
Dim colors As List(Of Color) = BuildGradient(chars.Count(Function(c) Not c.Equals(" "c)))
Dim sb As New StringBuilder
Dim compensator As Integer = 0
For i As Integer = 0 To chars.Count - 1
If Not chars(i).Equals(" "c) Then
sb.Append(String.Format("[color={0}]{1}[/color]", ColorTranslator.ToHtml(colors(i - compensator)), chars(i)))
Else
sb.Append(" "c)
compensator += 1
End If
Next
Return sb.ToString()
End Function
Private Function BuildGradient(stepCount As Integer) As List(Of Color)
Dim rMin As Integer = EndColor.R, rMax As Integer = StartColor.R
Dim gMin As Integer = EndColor.G, gMax As Integer = StartColor.G
Dim bMin As Integer = EndColor.B, bMax As Integer = StartColor.B
Dim colors As New List(Of Color)
For i As Integer = 0 To stepCount - 1
Dim rAvg As Integer = rMax + Convert.ToInt32((rMin - rMax) * i / stepCount)
Dim gAvg As Integer = gMax + Convert.ToInt32((gMin - gMax) * i / stepCount)
Dim bAvg As Integer = bMax + Convert.ToInt32((bMin - bMax) * i / stepCount)
colors.Add(Color.FromArgb(rAvg, gAvg, bAvg))
Next
Return colors
End Function
End Class