-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (94 loc) · 3.08 KB
/
Program.cs
File metadata and controls
101 lines (94 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using SkiaSharp;
public class FileReader
{
public static string ReadTextFromFile(string filePath)
{
return File.ReadAllText(filePath);
}
}
class Program
{
public static Dictionary<string, SKColor> Colors { get; } = new Dictionary<string, SKColor>
{
["син"] = SKColors.Blue,
["крас"] = SKColors.Red,
["зел"] = SKColors.Green,
["желт"] = SKColors.Yellow,
["бел"] = SKColors.White,
["черн"] = SKColors.Black,
["оранж"] = SKColors.Orange,
["фиолет"] = SKColors.Purple,
["бирюз"] = SKColors.Turquoise,
["ультрамарин"] = SKColors.Indigo,
["небесн"] = SKColors.SkyBlue,
["борд"] = SKColors.Maroon,
["изумруд"] = SKColors.Green,
["лайм"] = SKColors.Lime,
["золот"] = SKColors.Gold,
["серебр"] = SKColors.Silver,
["роз"] = SKColors.Pink,
["сер"] = SKColors.Gray,
["коричн"] = SKColors.Brown,
["голуб"] = SKColors.LightBlue
};
static void Main()
{
string filePath = @"/Users/lamaks/Documents/CodeSpace/C#/TextReader/test_book.txt";
string fileContent = FileReader.ReadTextFromFile(filePath);
if (fileContent != null)
{
Console.WriteLine($"File opened. Length: {fileContent.Length}");
FindAndCountColors(fileContent);
}
}
static void FindAndCountColors(string text)
{
List<SKColor> ColorsList = new List<SKColor>();
var words = text.Split(new[] { ' ', '.', ',', '!', '?', ';', ':', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
{
string lowerWord = word.ToLower();
foreach (var colorPair in Colors)
{
if (lowerWord.Contains(colorPair.Key))
{
ColorsList.Add(colorPair.Value);
break;
}
}
}
Console.WriteLine($"Number of colors found: {ColorsList.Count}");
Draw(ColorsList.Count, ColorsList);
}
static void Draw(int ColorsNumber,List<SKColor> ColorList)
{
int size = (int)Math.Ceiling(Math.Sqrt(ColorsNumber));
using (var bitmap = new SKBitmap(size, size))
{
int color_index = 0;
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (color_index < ColorList.Count)
{
bitmap.SetPixel(x, y, ColorList[color_index]);
color_index++;
}
else
{
break;
}
}
if (color_index >= ColorList.Count) break;
}
using (var image = SKImage.FromBitmap(bitmap))
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
using (var stream = File.OpenWrite("photo_res.png"))
{
data.SaveTo(stream);
}
}
Console.WriteLine("Image saved as photo_res.png");
}
}