@delirius/welcard
Version:
WelCard is a lightweight and futuristic welcome card library designed for WhatsApp Bots.
47 lines (46 loc) • 2.56 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.colorFetch = void 0;
const color_thief_node_1 = require("@delirius/color-thief-node");
const rgbToHex_1 = require("./rgbToHex");
const adjustBrightness_1 = require("./adjustBrightness");
/**
* Fetches and adjusts the color from a thumbnail image, converting it to hexadecimal format.
*
* @param color - The color option; if "auto", the function will fetch the dominant color from the thumbnail.
* @param brightness - The amount to adjust the brightness by (can be positive or negative).
* @param thumbnail - The URL of the thumbnail image to analyze.
* @returns A promise that resolves to a string representing the hexadecimal color value (without the '#') or a default value if an error occurs.
*/
function colorFetch(color, brightness, thumbnail) {
return __awaiter(this, void 0, void 0, function* () {
if (color === "auto") {
try {
// Fetch the dominant color from the thumbnail image
const dominantColor = yield (0, color_thief_node_1.getColorFromURL)(thumbnail);
const [red, green, blue] = dominantColor;
// Adjust the brightness of the color
const adjustedPalette = yield (0, adjustBrightness_1.adjustBrightness)(red, green, blue, brightness);
// Convert the adjusted color to hexadecimal format
const hexColor = yield (0, rgbToHex_1.rgbToHex)(...adjustedPalette);
return hexColor.replace("#", "");
}
catch (_a) {
// Return a default color value if an error occurs
return "03fc7f";
}
}
// Return an empty string if color is not "auto"
return "";
});
}
exports.colorFetch = colorFetch;