fish-station/Content.Client/_Sunrise/UserInterface/Controls/RichTextButton.cs
ThereDrD 6dd451a5c7
Панель телепорта призрака (#2157)
Co-authored-by: Vigers Ray <60344369+VigersRay@users.noreply.github.com>
2025-05-27 01:34:29 +03:00

48 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Robust.Client.UserInterface.Controls;
namespace Content.Client._Sunrise.UserInterface.Controls;
public sealed class RichTextButton : Button
{
public new SunriseRichTextLabel Label { get; }
public RichTextButton()
{
Label = new SunriseRichTextLabel();
AddChild(Label);
}
// TODO: Мб вынести эти два метода как экстеншен класс для строки/цвета.
// Или перенести куда, где это можно будет использовать извне
/// <summary>
/// Изменяет цвет текста, чтобы он был читаем на фоне кнопки
/// </summary>
private static string MakeTextReadable(string text, Color backgroundColor)
{
var newTextColor = GetReadableTextColor(backgroundColor);
var colorizedText = $"[color={newTextColor}]{text}[/color]";
return colorizedText;
}
/// <summary>
/// Возвращает белый или чёрный цвет, обеспечивающий читаемость на заданном фоне.
/// </summary>
private static string GetReadableTextColor(Color background)
{
// Вычисляем относительную яркость
var brightness = 0.299 * background.R +
0.587 * background.G +
0.114 * background.B;
return brightness > 0.85f ? Color.Black.ToHex() : Color.White.ToHex();
}
[ViewVariables]
public new string Text
{
get => Label.Text ?? string.Empty;
set => Label.Text = MakeTextReadable(value, ModulateSelfOverride ?? Modulate);
}
}