Skip to content

Commit 0c31c98

Browse files
committed
enhance: ColorPicker performance improvement
1 parent f4f0fe7 commit 0c31c98

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/Views/ColorPicker.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,24 @@ public override void Render(DrawingContext context)
9494

9595
// Color table.
9696
{
97+
// Colors
9798
for (int i = 0; i < 6; i++)
9899
{
99100
for (int j = 0; j < 8; j++)
100-
{
101-
var idx = i * 8 + j;
102-
var x = j * 32.0;
103-
var y = i * 32.0;
104-
context.FillRectangle(new SolidColorBrush(COLOR_TABLE[i, j]), new Rect(x, y, 32, 32));
105-
106-
if (idx == _hightlightedTableElement)
107-
context.DrawRectangle(new Pen(Brushes.White, 2), new Rect(x + 2, y + 2, 28, 28));
108-
}
101+
context.FillRectangle(new SolidColorBrush(COLOR_TABLE[i, j]), new Rect(j * 32, i * 32, 32, 32));
109102
}
110103

104+
// Borders
111105
var border = this.FindResource("Brush.Border0") as IBrush;
112106
var pen = new Pen(border, 0.4);
113107
for (int i = 1; i < 6; i++)
114108
context.DrawLine(pen, new Point(0, i * 32), new Point(256, i * 32));
115109
for (int j = 1; j < 8; j++)
116110
context.DrawLine(pen, new Point(j * 32, 0), new Point(j * 32, 192));
111+
112+
// Selected
113+
if (_hightlightedTableRect is { } rect)
114+
context.DrawRectangle(new Pen(Brushes.White, 2), rect);
117115
}
118116

119117
// Palette picker
@@ -142,6 +140,12 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
142140
}
143141
}
144142

143+
protected override void OnDataContextChanged(EventArgs e)
144+
{
145+
base.OnDataContextChanged(e);
146+
_hightlightedTableRect = null;
147+
}
148+
145149
protected override Size MeasureOverride(Size availableSize)
146150
{
147151
return new Size(256, 256);
@@ -154,12 +158,12 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
154158
var p = e.GetPosition(this);
155159
if (_colorTableRect.Contains(p))
156160
{
157-
int col = (int)Math.Floor(p.X / 32.0);
158-
int row = (int)Math.Floor(p.Y / 32.0);
159-
int idx = row * 8 + col;
160-
if (_hightlightedTableElement != idx)
161+
var col = (int)Math.Floor(p.X / 32.0);
162+
var row = (int)Math.Floor(p.Y / 32.0);
163+
var rect = new Rect(col * 32 + 2, row * 32 + 2, 28, 28);
164+
if (!rect.Equals(_hightlightedTableRect))
161165
{
162-
_hightlightedTableElement = idx;
166+
_hightlightedTableRect = rect;
163167
SetCurrentValue(ValueProperty, COLOR_TABLE[row, col].ToUInt32());
164168
}
165169

@@ -168,32 +172,32 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
168172

169173
if (_darkestRect.Rect.Contains(p))
170174
{
171-
_hightlightedTableElement = -1;
175+
_hightlightedTableRect = null;
172176
SetCurrentValue(ValueProperty, _darkestColor.ToUInt32());
173177
}
174178
else if (_darkerRect.Contains(p))
175179
{
176-
_hightlightedTableElement = -1;
180+
_hightlightedTableRect = null;
177181
SetCurrentValue(ValueProperty, _darkerColor.ToUInt32());
178182
}
179183
else if (_darkRect.Contains(p))
180184
{
181-
_hightlightedTableElement = -1;
185+
_hightlightedTableRect = null;
182186
SetCurrentValue(ValueProperty, _darkColor.ToUInt32());
183187
}
184188
else if (_lightRect.Contains(p))
185189
{
186-
_hightlightedTableElement = -1;
190+
_hightlightedTableRect = null;
187191
SetCurrentValue(ValueProperty, _lightColor.ToUInt32());
188192
}
189193
else if (_lighterRect.Contains(p))
190194
{
191-
_hightlightedTableElement = -1;
195+
_hightlightedTableRect = null;
192196
SetCurrentValue(ValueProperty, _lighterColor.ToUInt32());
193197
}
194198
else if (_lightestRect.Rect.Contains(p))
195199
{
196-
_hightlightedTableElement = -1;
200+
_hightlightedTableRect = null;
197201
SetCurrentValue(ValueProperty, _lightestColor.ToUInt32());
198202
}
199203
}
@@ -230,8 +234,7 @@ private Color GetNextColor(HsvColor c, double step)
230234
private Rect _lightRect = new Rect(160, 200, 32, 32);
231235
private Rect _lighterRect = new Rect(192, 200, 32, 32);
232236
private RoundedRect _lightestRect = new RoundedRect(new Rect(224, 200, 32, 32), new CornerRadius(0, 4, 4, 0));
233-
234-
private int _hightlightedTableElement = -1;
237+
private Rect? _hightlightedTableRect = null;
235238

236239
private Color _darkestColor;
237240
private Color _darkerColor;

0 commit comments

Comments
 (0)