|
|
#!/usr/bin/env node |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { execSync } = require( 'child_process' ); |
|
|
const { readFileSync } = require( 'fs' ); |
|
|
const path = require( 'path' ); |
|
|
const PALETTE = require( '@automattic/color-studio' ); |
|
|
const chroma = require( 'chroma-js' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const compareByName = ( objA, objB ) => { |
|
|
if ( objA.to.name > objB.to.name ) { |
|
|
return 1; |
|
|
} else if ( objB.to.name > objA.to.name ) { |
|
|
return -1; |
|
|
} |
|
|
return 0; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const pickBy = ( palette, fn ) => |
|
|
Object.keys( palette ?? {} ) |
|
|
.filter( ( key ) => fn( palette[ key ], key ) ) |
|
|
.reduce( ( acc, key ) => ( ( acc[ key ] = palette[ key ] ), acc ), {} ); |
|
|
|
|
|
|
|
|
const PALETTE_ILLUSTRATION_COLORS = pickBy( PALETTE.colors, ( colorValue, colorName ) => { |
|
|
|
|
|
if ( colorValue === '#000' ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
return ! colorName.startsWith( 'Simplenote Blue' ); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
const PALETTE_APP_COLORS = pickBy( PALETTE.colors, ( colorValue, colorName ) => { |
|
|
|
|
|
if ( colorValue === '#000' ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
return ! ( |
|
|
colorName.startsWith( 'Simplenote Blue' ) || |
|
|
colorName.startsWith( 'WooCommerce Purple' ) || |
|
|
colorName.startsWith( 'WordPress Blue' ) |
|
|
); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
const PALETTE_ILLUSTRATION_COLOR_VALUES = [ |
|
|
...new Set( Object.values( PALETTE_ILLUSTRATION_COLORS ) ), |
|
|
]; |
|
|
const PALETTE_APP_COLOR_VALUES = [ ...new Set( Object.values( PALETTE_APP_COLORS ) ) ]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SVG_IGNORE_PATHS = [ |
|
|
|
|
|
/(?:billcom|canva|evernote|facebook-messenger|fiverr|google-photos|monday|paypal|quickbooks|sendinblue|stripe|todoist|vaultpress)(-logo)?\.svg$/, |
|
|
/images\/g-suite\/logo_/, |
|
|
/images\/email-providers\/google-workspace/, |
|
|
|
|
|
|
|
|
/images\/customer-home\/illustration--task-connect-social-accounts.svg/, |
|
|
|
|
|
|
|
|
/upgrades\/cc-(?:amex|diners|discover|jcb|mastercard|unionpay|visa)\.svg$/, |
|
|
/upgrades\/(?:alipay|bancontact|emergent-paywall|eps|ideal|netbanking|p24|paypal|paytm|sofort|tef|wechat|razorpay)/, |
|
|
|
|
|
|
|
|
/color-scheme-thumbnail-(?:blue|classic-dark|coffee|ectoplasm|light|modern|ocean|sunrise)\.svg$/, |
|
|
|
|
|
|
|
|
/ninja-joy\.svg$/, |
|
|
|
|
|
|
|
|
/^static\/images\/marketing/, |
|
|
/^static\/images\/me/, |
|
|
/^static\/images\/illustrations\/illustration-woo-magic-link.svg/, |
|
|
/^static\/images\/jetpack\/favicons/, |
|
|
|
|
|
|
|
|
/^docs/, |
|
|
/^client\/my-sites\/checkout\/docs/, |
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
const SVG_APP_PATHS = [ |
|
|
|
|
|
/color-scheme-thumbnail-[a-z-]+\.svg$/, |
|
|
|
|
|
|
|
|
/^client\/components\/jetpack\/daily-backup-status\/status-card/, |
|
|
|
|
|
|
|
|
/^static\/images\/plans\//, |
|
|
]; |
|
|
|
|
|
|
|
|
const SVG_VALUE_EXPRESSION = |
|
|
/(?:fill|flood-color|lighting-color|stop-color|stroke)="([a-z0-9#]*?)"/gi; |
|
|
const SVG_STYLE_EXPRESSION = |
|
|
/(?:fill|flood-color|lighting-color|stop-color|stroke):\s*([a-z0-9#]*?)\s*[;}]/gi; |
|
|
|
|
|
|
|
|
|
|
|
const SVG_IGNORE_VALUES = [ 'currentcolor', 'none', 'transparent', 'white', '#ffffff' ]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ROOT_PATH = path.join( __dirname, '..' ); |
|
|
const GIT_DIR_PATH = path.join( ROOT_PATH, '.git' ); |
|
|
|
|
|
const SVG_FILES_TO_PROCESS = execSync( `git --git-dir="${ GIT_DIR_PATH }" ls-files "*.svg"` ) |
|
|
.toString() |
|
|
.trim() |
|
|
.split( /\s+/ ) |
|
|
.filter( excludeSelectedPaths ); |
|
|
|
|
|
const REPLACEMENT_RULES = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SVG_FILES_TO_PROCESS.forEach( ( imagePath ) => { |
|
|
const targetPreset = isAppImagePath( imagePath ) ? 'app' : 'illustration'; |
|
|
const targetValues = |
|
|
targetPreset === 'app' ? PALETTE_APP_COLOR_VALUES : PALETTE_ILLUSTRATION_COLOR_VALUES; |
|
|
|
|
|
const imageContent = getFileContents( imagePath ); |
|
|
const matchedColorValues = matchColorValues( imageContent ); |
|
|
const colorValuesToReplace = []; |
|
|
|
|
|
[ ...new Set( matchedColorValues ) ].forEach( ( value ) => { |
|
|
if ( SVG_IGNORE_VALUES.includes( value ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( targetValues.includes( value ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( colorValuesToReplace.includes( value ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
colorValuesToReplace.push( value ); |
|
|
} ); |
|
|
|
|
|
if ( ! colorValuesToReplace.length ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
REPLACEMENT_RULES.push( { |
|
|
file: imagePath, |
|
|
preset: targetPreset, |
|
|
rules: colorValuesToReplace |
|
|
.filter( ( val ) => val !== 'null' ) |
|
|
.map( ( value ) => { |
|
|
const replacementValue = findClosestColor( value, targetValues ); |
|
|
const replacementName = findPaletteColorName( replacementValue ); |
|
|
|
|
|
return { |
|
|
from: { |
|
|
value, |
|
|
}, |
|
|
to: { |
|
|
value: replacementValue, |
|
|
name: replacementName, |
|
|
}, |
|
|
}; |
|
|
} ), |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printReplacementRules( REPLACEMENT_RULES ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function excludeSelectedPaths( imagePath ) { |
|
|
|
|
|
return SVG_IGNORE_PATHS.every( ( ignoredPath ) => { |
|
|
return ! ignoredPath.test( imagePath ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function isAppImagePath( imagePath ) { |
|
|
return SVG_APP_PATHS.some( ( appPath ) => { |
|
|
return appPath.test( imagePath ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function getFileContents( filePath ) { |
|
|
return readFileSync( path.join( ROOT_PATH, filePath ) ).toString().trim(); |
|
|
} |
|
|
|
|
|
function matchColorValues( content ) { |
|
|
const values = []; |
|
|
let match; |
|
|
|
|
|
[ SVG_VALUE_EXPRESSION, SVG_STYLE_EXPRESSION ].forEach( ( expression ) => { |
|
|
|
|
|
while ( ( match = expression.exec( content ) ) !== null ) { |
|
|
values.push( match[ 1 ].toLowerCase() ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
return values; |
|
|
} |
|
|
|
|
|
function findClosestColor( value, targetValues ) { |
|
|
let closestValue = value; |
|
|
let closestDistance = Infinity; |
|
|
let targetValue; |
|
|
|
|
|
for ( targetValue of targetValues ) { |
|
|
const distance = chroma.distance( value, targetValue ); |
|
|
|
|
|
|
|
|
|
|
|
if ( distance === 0 ) { |
|
|
closestValue = targetValue; |
|
|
closestDistance = distance; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
if ( targetValue === '#fff' ) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
if ( distance < closestDistance ) { |
|
|
closestValue = targetValue; |
|
|
closestDistance = distance; |
|
|
} |
|
|
} |
|
|
|
|
|
return closestValue; |
|
|
} |
|
|
|
|
|
function findPaletteColorName( value ) { |
|
|
|
|
|
const name = Object.keys( PALETTE.colors ?? {} ).findLast( ( paletteColorName ) => { |
|
|
return PALETTE.colors[ paletteColorName ] === value; |
|
|
} ); |
|
|
return name; |
|
|
} |
|
|
|
|
|
function printReplacementRules( replacementObjects ) { |
|
|
const count = replacementObjects.length; |
|
|
|
|
|
if ( count <= 0 ) { |
|
|
console.log( |
|
|
`All the SVG illustration files in this repository seem to use correct color values. ✨` |
|
|
); |
|
|
} else { |
|
|
console.log( |
|
|
`Found ${ count } SVG images in this repository that use non-standard color values:` |
|
|
); |
|
|
|
|
|
replacementObjects.forEach( ( replacementObject ) => { |
|
|
const replacementRules = formatReplacementRules( replacementObject.rules ); |
|
|
const replacementSuffix = getReplacementPrefixSuffix( replacementObject ); |
|
|
console.log( |
|
|
`\n${ replacementObject.file }${ replacementSuffix }\n${ replacementRules.join( '\n' ) }` |
|
|
); |
|
|
} ); |
|
|
} |
|
|
} |
|
|
|
|
|
function formatReplacementRules( rules ) { |
|
|
if ( rules && rules.length ) { |
|
|
return [ ...rules ].sort( compareByName ).map( ( rule ) => { |
|
|
const valueFrom = rule.from.value.padEnd( 7 ); |
|
|
const valueTo = rule.to.value.padEnd( 7 ); |
|
|
|
|
|
return `${ valueFrom } → ${ valueTo } (${ rule.to.name })`; |
|
|
} ); |
|
|
} |
|
|
return []; |
|
|
} |
|
|
|
|
|
function getReplacementPrefixSuffix( replacementObject ) { |
|
|
const { preset } = replacementObject; |
|
|
return preset === 'app' ? ' 🖥' : ''; |
|
|
} |
|
|
|