Skip to content

Instantly share code, notes, and snippets.

@weedgrease
Created April 6, 2017 17:40
Show Gist options
  • Save weedgrease/ac909d137cd22aae509fdb535918cd1d to your computer and use it in GitHub Desktop.
Save weedgrease/ac909d137cd22aae509fdb535918cd1d to your computer and use it in GitHub Desktop.
Cam's Project
getYourCards(numbersOnly) {
fs.createReadStream(this.imagePath).pipe(new PNG({
filterType: -1
})).on("parsed", function() {
let yourCardsSuit = [];
console.log("Array created");
console.log("Entered read stream");
// Loop through each vertical pixel of the image
for (var y = 0; y < this.height; y++) {
// Loop through each horizontal pixel of the image
for (var x = 0; x < this.width; x++) {
// Give current pixel 4 bytes 0: R, 1: G, 2:B, 3:A
var idx = (this.width * y + x) << 2;
// Check if we are looking for numbers or suits
if (numbersOnly) {
// Add an overlay to the image for better reading
// Bring the overlay down to this y level
if (y <= 679) {
this.data[idx] = 255;
this.data[idx + 1] = 255;
this.data[idx + 2] = 255;
}
// Bring the overlay over from the left
if (x <= 670) {
this.data[idx] = 255;
this.data[idx + 1] = 255;
this.data[idx + 2] = 255;
}
// Bring the overlay up from the bottom
if (y >= this.height - 367) {
this.data[idx] = 255;
this.data[idx + 1] = 255;
this.data[idx + 2] = 255;
}
// Bring the overlay from the right
if (x >= this.width - 1080) {
this.data[idx] = 255;
this.data[idx + 1] = 255;
this.data[idx + 2] = 255;
}
// Puts an overlay between the cards
if (x >= 715 && x <= 765) {
this.data[idx] = 255;
this.data[idx + 1] = 255;
this.data[idx + 2] = 255;
}
if (this.data[idx] < 230 || this.data[idx + 1] < 230 || this.data[
idx + 2] < 230) {
this.data[idx] = 0;
this.data[idx + 1] = 0;
this.data[idx + 2] = 0;
}
// If we are looking for the suit
} else {
// if the x and y are at this position
// This position is the left card of the hand
if (x == 685 && y == 726) {
// random calculation to sort colors by 1 number
var color = this.data[idx] + this.data[idx + 1] + this.data[
idx + 2];
// If the green in a card is less than 10 add 47. this is for hearts
if (this.data[idx + 1] <= 10) {
color += 47;
}
switch (true) {
// Club
case (color > 201 && color < 221):
yourCardsSuit.push("C");
this.newHand = true;
break;
// Diamond
case (color > 294 && color < 314):
yourCardsSuit.push("D");
this.newHand = true;
break;
// Spade
case (color > 140 && color < 160):
yourCardsSuit.push("S");
this.newHand = true;
break;
// Heart
case (color > 252 && color < 272):
yourCardsSuit.push("H");
this.newHand = true;
break;
default:
console.log("No first card found");
this.newHand = false;
}
}
// If x and y are on the second card
if (x == 777 && y == 726) {
// random calculation to sort colors by 1 number
var color = this.data[idx] + this.data[idx + 1] + this.data[
idx + 2];
// If the green in a card is less than 10 add 47. this is for hearts
if (this.data[idx + 1] <= 10) {
color += 47;
}
switch (true) {
// Club
case (color > 201 && color < 221):
yourCardsSuit.push("C");
this.newHand = true;
break;
// Diamond
case (color > 294 && color < 314):
yourCardsSuit.push("D");
this.newHand = true;
break;
// Spade
case (color > 140 && color < 160):
yourCardsSuit.push("S");
this.newHand = true;
break;
// Heart
case (color > 252 && color < 272):
yourCardsSuit.push("H");
this.newHand = true;
break;
default:
console.log("No first card found");
this.newHand = false;
}
}
}
}
}
if (numbersOnly) {
this.pack().pipe(fs.createWriteStream('./images/yourHand.png'));
}
console.log("returned array");
console.log(yourCardsSuit);
return yourCardsSuit;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment