predict-color
Version:
150 lines (149 loc) • 4.8 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const jimp_1 = __importDefault(require("jimp"));
/**
*
* @module color-predictor
*/
const calcAvg = (arr) => arr.reduce((acc, num) => acc + num, 0) / (arr.length - 1);
/**
*
* @param url
* @returns color
*/
const colorFromURL = (url) => __awaiter(void 0, void 0, void 0, function* () {
try {
let color = "";
const red = [];
const green = [];
const blue = [];
const image = yield jimp_1.default.read(url);
image.cover(224, 224, jimp_1.default.HORIZONTAL_ALIGN_CENTER | jimp_1.default.VERTICAL_ALIGN_MIDDLE);
image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, _) => {
const pixel = jimp_1.default.intToRGBA(image.getPixelColor(x, y));
red.push(pixel.r);
green.push(pixel.g);
blue.push(pixel.b);
});
const [redavg, greeenavg, blueavg] = yield Promise.all([
calcAvg(red),
calcAvg(green),
calcAvg(blue),
]);
if (redavg > greeenavg && redavg > blueavg) {
color = "red";
}
else if (greeenavg > redavg && greeenavg > blueavg) {
color = "green";
}
else if (blueavg > redavg && blueavg > greeenavg) {
color = "blue";
}
else {
throw Error("something went wrong");
}
return color;
}
catch (error) {
console.log(error);
return "something went wrong";
}
});
/**
*
* @param reds
* @param greens
* @param blues
*/
const colorFromRGB = (r, g, b) => __awaiter(void 0, void 0, void 0, function* () {
try {
let color = "";
const [redavg, greeenavg, blueavg] = yield Promise.all([
calcAvg(r),
calcAvg(g),
calcAvg(b),
]);
if (redavg > greeenavg && redavg > blueavg) {
color = "red";
}
else if (greeenavg > redavg && greeenavg > blueavg) {
color = "green";
}
else if (blueavg > redavg && blueavg > greeenavg) {
color = "blue";
}
else {
throw Error("something went wrong with the color");
}
return color;
}
catch (error) {
console.log(error);
throw Error("something went wrong with color");
}
});
/**
*
* @param path - path to image
* @returns color
*/
const colorFromImage = (path) => __awaiter(void 0, void 0, void 0, function* () {
try {
if (!fs_1.default.existsSync(path)) {
throw Error("image not found");
}
let color = "";
const red = [];
const green = [];
const blue = [];
const image = yield jimp_1.default.read(path);
image.cover(224, 224, jimp_1.default.HORIZONTAL_ALIGN_CENTER | jimp_1.default.VERTICAL_ALIGN_MIDDLE);
image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, _) => {
const pixel = jimp_1.default.intToRGBA(image.getPixelColor(x, y));
red.push(pixel.r);
green.push(pixel.g);
blue.push(pixel.b);
});
const [redavg, greeenavg, blueavg] = yield Promise.all([
calcAvg(red),
calcAvg(green),
calcAvg(blue),
]);
if (redavg > greeenavg && redavg > blueavg) {
color = "red";
}
else if (greeenavg > redavg && greeenavg > blueavg) {
color = "green";
}
else if (blueavg > redavg && blueavg > greeenavg) {
color = "blue";
}
else {
throw Error("something went wrong");
}
return color;
}
catch (error) {
console.log(error);
return "something went wrong";
}
});
const predict = {
colorFromURL,
colorFromRGB,
colorFromImage
};
exports.default = predict;
;