| 1 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 2 |
|
| 3 |
/** Removes any hash or qs values from URL - Airtable adds timestamps */ |
| 4 |
export const stripUrlParams = (url) => url?.[0]?.url?.split(/[?#]/)?.[0]; |
| 5 |
|
| 6 |
function cleanAndBuildUnsplashUrl(url) { |
| 7 |
const cleanUrl = url |
| 8 |
.replaceAll('\\u0026', '&') |
| 9 |
// Remove duplicate question marks in URL by replacing second '?' with '&' |
| 10 |
.replace(/(\?.*?)\?/, '$1&'); |
| 11 |
let imageUrl = new URL(decodeEntities(cleanUrl)); |
| 12 |
|
| 13 |
const size = 1440; |
| 14 |
const orientation = imageUrl.searchParams.get('orientation'); |
| 15 |
|
| 16 |
if (orientation === 'portrait') { |
| 17 |
imageUrl.searchParams.set('h', size); |
| 18 |
imageUrl.searchParams.delete('w'); |
| 19 |
} else if (orientation === 'landscape' || orientation === 'square') { |
| 20 |
const widthParam = imageUrl.searchParams.get('w'); |
| 21 |
if (widthParam === null || widthParam === '') { |
| 22 |
imageUrl.searchParams.set('w', size); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
imageUrl.searchParams.delete('orientation'); |
| 27 |
imageUrl.searchParams.delete('ixid'); |
| 28 |
imageUrl.searchParams.delete('ixlib'); |
| 29 |
imageUrl.searchParams.append('q', '0'); |
| 30 |
imageUrl.searchParams.append('auto', 'format,compress'); |
| 31 |
imageUrl.searchParams.append('fm', 'avif'); |
| 32 |
return imageUrl.toString(); |
| 33 |
} |
| 34 |
|
| 35 |
export const lowerImageQuality = (html) => { |
| 36 |
return html.replace( |
| 37 |
/https:\/\/images\.unsplash\.com\/[^"')]+/g, |
| 38 |
cleanAndBuildUnsplashUrl, |
| 39 |
); |
| 40 |
}; |
| 41 |
|
| 42 |
export const hexTomatrixValues = (hex) => { |
| 43 |
// convert from hex |
| 44 |
const colorInt = parseInt(hex.replace('#', ''), 16); |
| 45 |
// convert to rgb |
| 46 |
// This shifts each primary color value to the right-most 8 bits |
| 47 |
// then applies a mask to get the value of that color |
| 48 |
const r = (colorInt >> 16) & 255; |
| 49 |
const g = (colorInt >> 8) & 255; |
| 50 |
const b = colorInt & 255; |
| 51 |
// normalize to 0-1 |
| 52 |
return [ |
| 53 |
Math.round((r / 255) * 10000) / 10000, |
| 54 |
Math.round((g / 255) * 10000) / 10000, |
| 55 |
Math.round((b / 255) * 10000) / 10000, |
| 56 |
]; |
| 57 |
}; |
| 58 |
|