Skip to content

Instantly share code, notes, and snippets.

@appletreeat56
Created November 7, 2022 10:44
Show Gist options
  • Save appletreeat56/afb9396f97a5fd216cfaf6d7033ee35a to your computer and use it in GitHub Desktop.
Save appletreeat56/afb9396f97a5fd216cfaf6d7033ee35a to your computer and use it in GitHub Desktop.
//cols: Width of the image representing total number of columns
//x: Row position of this pixel
//y: Column position of this pixel
const extractPixelColor = (cols: number, x: number, y: number) => {
//To get the exact pixel, the logic is to multiply total columns in this image with the row position of this pixel and then add the column position of this pixed
let pixel = (cols * x) + y;
//To get the array position in the entire image data array, simply multiply your pixel position by 4 (since each pixel will have its own r,g,b,a in that order)
let position = pixel * 4;
//the rgba value of current pixel will be the following
return {
red: data[position],
green: data[position + 1],
blue: data[position + 2],
alpha: data[position + 3],
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment