Created
July 12, 2019 10:38
-
-
Save dmitriydementor/11a9f1974481c6a76d94f2cbecd23443 to your computer and use it in GitHub Desktop.
generate angular material theme from only orimary and warn color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { execSync } from 'child_process'; | |
import { join } from 'path'; | |
import { readFileSync, writeFileSync } from 'fs'; | |
import * as sassExtract from 'sass-extract'; | |
const materilPalettePath = './src/modules/common/style/angular-material'; | |
const warnColor = '#ed1c24'; | |
sassExtract.render({ | |
file: `./src/modules/common/style/colors.scss`, | |
}).then(rendered => { | |
generatePaletteFile(rendered.vars.global.$mainColor.value.hex, materilPalettePath, 'primary.scss'); | |
replaceInFile( | |
join(materilPalettePath, 'primary.scss'), | |
'$palette', | |
'$primaryPalette' | |
); | |
const primaryColorHex: number = parseInt(rendered.vars.global.$mainColor.value.hex.replace('#', ''), 16); | |
const whiteColorHex: number = 0xffffff; | |
const accentColorHexStr: string = `#${convertRgbaToHex(primaryColorHex, 0.5, whiteColorHex)}`; | |
generatePaletteFile(accentColorHexStr, materilPalettePath, 'accent.scss'); | |
replaceInFile( | |
join(materilPalettePath, 'accent.scss'), | |
'$palette', | |
'$accentPalette' | |
); | |
generatePaletteFile(warnColor, materilPalettePath, 'warn.scss'); | |
replaceInFile( | |
join(materilPalettePath, 'warn.scss'), | |
'$palette', | |
'$warnPalette' | |
); | |
}); | |
function generatePaletteFile(color: string, path: string, filename: string) { | |
execSync(`material-palette-generator -c '${color}' --format material2 -d ${path} --file-name ${filename}`, { | |
maxBuffer: 1024 * 1024, | |
encoding: 'utf8', | |
stdio: 'inherit' | |
}); | |
} | |
function replaceInFile(path: string, searchValue: string | RegExp, replaceValue: string) { | |
let fileStr = readFileSync(path, 'utf8'); | |
fileStr = fileStr.replace(searchValue, replaceValue); | |
writeFileSync(path, fileStr, 'utf8'); | |
} | |
function convertRgbaToHex(color: number, alpha: number, backgroundColor: number): string { | |
return (color * alpha + backgroundColor * (1 - alpha)).toString(16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment