Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Linfindel committed Feb 7, 2024
1 parent 8357b05 commit 3a1d4bb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
18 changes: 1 addition & 17 deletions filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,15 @@ generateMaterialDesignPalette(url, (error, palette) => {
}
})

// Function to generate a Material Design color palette from an image URL
function generateMaterialDesignPalette(imageURL, callback) {
// Create an image element to load the image
const img = new Image();
img.crossOrigin = "Anonymous"; // Enable cross-origin access to the image
img.crossOrigin = "Anonymous";

// Set up an event listener for when the image is loaded
img.onload = function () {
// Create a Vibrant.js object to extract colors from the image
const vibrant = new Vibrant(img);
const swatches = vibrant.swatches();

// Check if swatches were successfully generated
if (swatches) {
// Extract Material Design color palette
const palette = {
accent: swatches.Vibrant.getHex(),
primaryDark: swatches.DarkVibrant.getHex(),
Expand All @@ -154,32 +148,25 @@ function generateMaterialDesignPalette(imageURL, callback) {

localStorage.setItem("accent", palette.accent);

// Execute the callback function with the generated palette
callback(null, palette);
}

else {
// Error handling if swatches couldn't be generated
callback("Failed to generate swatches", null);
}
};

// Set the image source to the provided URL
img.src = imageURL;
}

// Function to generate an RGBA value with a specified alpha
function generateRGBA(hex, alpha) {
// Remove the "#" symbol if present
hex = hex.replace(/^#/, '');

// Parse the hex color to RGB components
const bigint = parseInt(hex, 16);
const red = (bigint >> 16) & 255;
const green = (bigint >> 8) & 255;
const blue = bigint & 255;

// Create the RGBA string
return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
}

Expand Down Expand Up @@ -397,7 +384,6 @@ function compare() {
}

function calculateContrastRatio(foreground, background) {
// Calculate contrast ratio
const fgRgb = foreground;
const bgRgb = hexToRgb(background);

Expand All @@ -411,15 +397,13 @@ function calculateContrastRatio(foreground, background) {
}

function hexToRgb(hex) {
// Convert a hex color to RGB values
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return [r, g, b];
}

function getRelativeLuminance(rgb) {
// Calculate relative luminance
const [r, g, b] = rgb.map((c) => {
const sRGB = c / 255;
return sRGB <= 0.03928 ? sRGB / 12.92 : Math.pow((sRGB + 0.055) / 1.055, 2.4);
Expand Down
9 changes: 1 addition & 8 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const button2Icon = document.getElementById("button2-icon");
const button2Text = document.getElementById("button2-text");

const queryString = window.location.search;
console.log(queryString);
const urlParams = new URLSearchParams(queryString);
const urlParam = urlParams.get('url');

Expand All @@ -27,9 +26,7 @@ if (urlParam) {
console.error(error);
}

else {
console.log("Material Design Palette:", palette);

else {
// Set the background color of buttons with alpha 0.25
button1.style.backgroundColor = generateRGBA(palette.accent, 0.25);
button2.style.backgroundColor = generateRGBA(palette.accent, 0.25);
Expand Down Expand Up @@ -136,8 +133,6 @@ function uploadLink() {
}

else {
console.log("Material Design Palette:", palette);

// Set the background color of buttons with alpha 0.25
button1.style.backgroundColor = generateRGBA(palette.accent, 0.25);
button2.style.backgroundColor = generateRGBA(palette.accent, 0.25);
Expand Down Expand Up @@ -201,8 +196,6 @@ function uploadFile() {
}

else {
console.log("Material Design Palette:", palette);

// Set the background color of buttons with alpha 0.25
button1.style.backgroundColor = generateRGBA(palette.accent, 0.25);
button2.style.backgroundColor = generateRGBA(palette.accent, 0.25);
Expand Down

0 comments on commit 3a1d4bb

Please sign in to comment.