Skip to content

Commit

Permalink
(feat) improve icon quality
Browse files Browse the repository at this point in the history
  • Loading branch information
leonjza committed Feb 15, 2024
1 parent 1bdf7d3 commit 8e4af15
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 0 additions & 2 deletions SunSynkTray/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ public async Task RefreshAuthToken()

// set the auth header
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", auth.data.access_token);


}

public async Task<T> Get<T>(string endpoint)
Expand Down
7 changes: 4 additions & 3 deletions SunSynkTray/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private async void listBoxPlants_SelectedIndexChanged(object sender, EventArgs e
labelPlantStatus.Visible = true;

trayIcon.Text = statusText;
trayIcon.Icon = Tools.CreateIcon(energy.data.soc, energy.data.loadOrEpsPower);
trayIcon.Icon = Tools.TextIcon(energy.data.soc, Tools.PercentAsColor(energy.data.soc));

if (!timerCallApi.Enabled) timerCallApi.Start();
}
Expand Down Expand Up @@ -186,7 +186,7 @@ private async void timerCallApi_Tick(object sender, EventArgs e)
labelPlantStatus.Visible = true;

trayIcon.Text = statusText;
trayIcon.Icon = Tools.CreateIcon(energy.data.soc, energy.data.loadOrEpsPower);
trayIcon.Icon = Tools.TextIcon(energy.data.soc, Tools.PercentAsColor(energy.data.soc));

}
catch (Exception ex)
Expand All @@ -196,7 +196,8 @@ private async void timerCallApi_Tick(object sender, EventArgs e)
labelPlantStatus.Visible = true;

trayIcon.Text = statusText;
trayIcon.Icon = Tools.CreateIcon(0.0f, 0);
//trayIcon.Icon = Tools.CreateIcon(0.0f, 0);
trayIcon.Icon = Tools.TextIcon(0.0f, Tools.PercentAsColor(0.0f));
}
}
}
Expand Down
43 changes: 26 additions & 17 deletions SunSynkTray/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,53 @@ namespace SunSynkTray
{
internal static class Tools
{
public static Icon CreateIcon(float n1, int n2)
public static Icon TextIcon(float num, Color color)
{

int iconSize = 32;
int iconSize = 16;

using (Bitmap bitmap = new Bitmap(iconSize, iconSize))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// Enable smoothing of the edge of the circle and the text
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

graphics.Clear(Color.Transparent);
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;

// Set up the font and brush for drawing
// Adjust the font size as needed to make the number as large as possible while fitting in the icon
using (Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel))
using (Brush brush = new SolidBrush(Color.Yellow))
using (Font font = new Font("Tahoma", 10, FontStyle.Regular, GraphicsUnit.Pixel))
using (Brush brush = new SolidBrush(color))
{
StringFormat stringFormat = new StringFormat
{
Alignment = StringAlignment.Center, // Center text horizontally
LineAlignment = StringAlignment.Center, // Center text vertically
};

// Draw the number in the center of the bitmap
graphics.DrawString(n1.ToString(), font, brush, new RectangleF(0, 0, iconSize, iconSize), stringFormat);
graphics.DrawString(num.ToString(), font, brush, -2, 0);
}
}

// Convert the bitmap to an icon and return

using (Icon icon = Icon.FromHandle(bitmap.GetHicon()))
{
return (Icon)icon.Clone();
}
}
}

public static Color PercentAsColor(float p)
{
if (p >= 80)
{
return Color.Green;
}
else if ((p < 80) && (p >= 60))
{
return Color.Yellow;
}
else if ((p < 60) && (p >= 40))
{
return Color.Purple;
}
else
{
return Color.Red;
}
}

public static string TimeAgoFormat(DateTime time)
Expand Down
Binary file modified images/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8e4af15

Please sign in to comment.