Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2in15g WaveShare ePaper driver #1097

Merged
merged 8 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BufferIndexed2() : base() { }
public override void Fill(Color color)
{
byte colorValue = (byte)GetIndexForColor(color);
Buffer[0] = (byte)(colorValue << 2 | colorValue << 4 | colorValue << 6);
Buffer[0] = (byte)(colorValue | colorValue << 2 | colorValue << 4 | colorValue << 6);

int arrayMidPoint = Buffer.Length / 2;
int copyLength;
Expand Down Expand Up @@ -121,7 +121,7 @@ public override void SetPixel(int x, int y, Color color)
public void SetPixel(int x, int y, int colorIndex)
{
int byteIndex = (y * Width + x) >> 2; // divide by 4 to find the byte
int pixelOffset = (x & 0x03) << 1; // (x % 4)*2 bits offset
int pixelOffset = (3 - (x & 0x03)) << 1; // Reverse offset calculation

// Clear current 2 bits
Buffer[byteIndex] &= (byte)~(0x03 << pixelOffset);
Expand Down Expand Up @@ -184,11 +184,6 @@ public byte GetColorIndexForPixel(int x, int y)
return value;
}

Color GetClosestColor(Color color)
{
return IndexedColors[GetIndexForColor(color)];
}

int GetIndexForColor(Color color)
{
if (IndexedColors == null || IndexedColors.All(x => x == null))
Expand All @@ -204,6 +199,7 @@ int GetIndexForColor(Color color)
if (IndexedColors[i] != null)
{
double distance = GetColorDistance(color, IndexedColors[i]);

if (distance < shortestDistance)
{
shortestDistance = distance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,6 @@ public byte GetColorIndexForPixel(int x, int y)
return color;
}

Color GetClosestColor(Color color)
{
return IndexedColors[GetIndexForColor(color)];
}

int GetIndexForColor(Color color)
{
if (IndexedColors == null || IndexedColors.All(x => x == null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public int BitDepth
{
ColorMode.Format1bpp => 1,
ColorMode.Format2bpp => 2,
ColorMode.Format2bppIndexed => 2,
ColorMode.Format4bppGray => 4,
ColorMode.Format4bppIndexed => 4,
ColorMode.Format8bppGray => 8,
Expand Down Expand Up @@ -526,6 +527,11 @@ public T Crop<T>(int startX, int startY, int cropWidth, int cropHeight)
/// <returns>The distance as a float</returns>
public float GetColorDistance(Color color1, Color color2)
{
if (color1 == color2)
{
return 0;
}

var rDeltaSquared = MathF.Pow(MathF.Abs(color1.R - color2.R), 2);
var gDeltaSquared = MathF.Pow(MathF.Abs(color1.G - color2.G), 2);
var bDeltaSquared = MathF.Pow(MathF.Abs(color1.B - color2.B), 2);
Expand Down
Loading
Loading