2015-10-14 17:52:04 +02:00
// Get JSON from source file
var source = require ( './simple-icons.json' ) ;
// Loop through icons
for ( var i = 0 ; i < source . icons . length ; i ++ ) {
var hex = source . icons [ i ] . hex ;
// Add red, green and blue values to the JSON object
var red = parseInt ( hex . substr ( 0 , 2 ) , 16 ) / 255 ;
var green = parseInt ( hex . substr ( 2 , 2 ) , 16 ) / 255 ;
var blue = parseInt ( hex . substr ( 4 , 2 ) , 16 ) / 255 ;
// Add hue to the JSON object
var max = Math . max ( red , green , blue ) ;
var min = Math . min ( red , green , blue ) ;
var delta = max - min ;
2015-10-16 15:35:52 +02:00
source . icons [ i ] . luminance = 100 * ( max + min ) / 2 ;
2015-10-14 17:52:04 +02:00
if ( delta === 0 ) {
var hue = 0 ;
2015-10-16 16:43:13 +02:00
source . icons [ i ] . saturation = 0 ;
2015-10-14 17:52:04 +02:00
} else {
2015-10-16 16:43:13 +02:00
if ( source . icons [ i ] . luminance < 50 ) {
source . icons [ i ] . saturation = 100 * ( max - min ) / ( max + min ) ;
} else {
source . icons [ i ] . saturation = 100 * ( max - min ) / ( 2 - max - min ) ;
}
2015-10-14 17:52:04 +02:00
if ( max === red ) {
var hue = ( ( green - blue ) / delta ) * 60 ;
if ( hue < 0 ) {
hue += 360 ;
}
} else if ( max === green ) {
var hue = ( ( ( blue - red ) / delta ) + 2 ) * 60 ;
} else {
var hue = ( ( ( red - green ) / delta ) + 4 ) * 60 ;
}
}
source . icons [ i ] . hue = hue ;
}
// Sort icons by hue
2015-10-15 15:30:13 +02:00
for ( var i = 0 ; i < source . icons . length ; i ++ ) {
2015-11-10 13:22:45 +02:00
source . icons [ i ] . hue += 90 ;
2015-10-15 15:30:13 +02:00
source . icons [ i ] . hue = source . icons [ i ] . hue % 360 ;
}
2015-10-14 17:52:04 +02:00
source . icons . sort ( function ( a , b ) {
2015-10-22 14:26:56 +02:00
return parseFloat ( a . hue ) - parseFloat ( b . hue ) ;
2015-10-14 17:52:04 +02:00
} ) ;
2015-10-15 15:30:13 +02:00
var tmp = [ ] ;
for ( var i = 0 ; i < source . icons . length ; i ++ ) {
2015-11-13 19:15:15 +02:00
if ( source . icons [ i ] . luminance < 15 ) {
2015-10-15 15:30:13 +02:00
tmp . push ( source . icons [ i ] ) ;
source . icons . splice ( i , 1 ) ;
i -- ;
}
}
2015-10-16 15:35:52 +02:00
for ( var i = 0 ; i < source . icons . length ; i ++ ) {
2015-11-13 19:15:15 +02:00
if ( source . icons [ i ] . saturation < 25 ) {
2015-10-16 15:35:52 +02:00
tmp . push ( source . icons [ i ] ) ;
source . icons . splice ( i , 1 ) ;
i -- ;
}
}
2015-10-15 15:30:13 +02:00
tmp . sort ( function ( a , b ) {
return parseFloat ( b . luminance ) - parseFloat ( a . luminance ) ;
} ) ;
for ( var i = 0 ; i < tmp . length ; i ++ ) {
source . icons . push ( tmp [ i ] ) ;
}
2015-10-14 17:52:04 +02:00
// Read header and footer content into variables
var fs = require ( 'fs' ) ;
function readFile ( path , callback ) {
try {
var filename = require . resolve ( path ) ;
fs . readFile ( filename , 'utf8' , callback ) ;
} catch ( e ) {
callback ( e ) ;
}
}
var fs = require ( 'fs' ) ;
2015-10-22 15:57:08 +02:00
var header = fs . readFileSync ( './header.html' , 'utf8' ) ;
var footer = fs . readFileSync ( './footer.html' , 'utf8' ) ;
2015-10-14 17:52:04 +02:00
2015-10-15 15:30:13 +02:00
// Build content
2015-10-28 16:57:38 +02:00
var main = " <p class=\"hero\">" + source . icons . length + " SVG icons for popular brands <a href=\"https://github.com/danleech/simple-icons\">Download them from GitHub</a></p>\n <input type=\"text\" id=\"search\" class=\"search-field\" autofocus>\n <ul class=\"tiles\">" ;
2015-10-15 15:30:13 +02:00
2015-10-14 17:52:04 +02:00
for ( var i = 0 ; i < source . icons . length ; i ++ ) {
2015-10-17 12:38:33 +02:00
var fileName = source . icons [ i ] . title . toLowerCase ( ) ;
2015-11-17 13:04:56 +02:00
fileName = fileName . replace ( /[ |!|.]/g , '' ) ;
fileName = fileName . replace ( /[+]/ , 'plus' ) ;
2015-10-22 15:57:08 +02:00
filePath = "../icons/" + fileName + ".svg" ;
2015-10-15 15:30:13 +02:00
var fs = require ( 'fs' ) ;
var svg = fs . readFileSync ( filePath , 'utf8' ) ;
2015-11-09 19:56:37 +02:00
main += "\n <li class=\"tiles__item\" data-search=\"" + source . icons [ i ] . title . toLowerCase ( ) + " " + fileName . toLowerCase ( ) + " " + source . icons [ i ] . hex . toLowerCase ( ) + "\" style=\"background-color:#" + source . icons [ i ] . hex + "\"><a href=\"https://simpleicons.org/icons/" + fileName + ".svg\">" + svg + "</a><span class=\"tile-name\">" + source . icons [ i ] . title + "</span>" + "<br><span class=\"hex\">#" + source . icons [ i ] . hex + "</span></li>" ;
2015-10-14 17:52:04 +02:00
}
// Put all content together and export to index.html
var htmlOutput = header + main + footer ;
2015-10-22 15:57:08 +02:00
fs . writeFile ( "../index.html" , htmlOutput , function ( err ) {
2015-10-14 17:52:04 +02:00
if ( err ) {
return console . log ( err ) ;
}
2015-10-26 17:20:31 +02:00
console . log ( "The index.html file was saved!" ) ;
2015-10-19 12:08:22 +02:00
} ) ;
2015-10-19 17:38:48 +02:00
// Also output to 404.html
2015-10-22 15:57:08 +02:00
fs . writeFile ( "../404.html" , htmlOutput , function ( err ) {
2015-10-19 17:38:48 +02:00
if ( err ) {
return console . log ( err ) ;
}
2015-10-26 17:20:31 +02:00
console . log ( "The 404.html file was saved!" ) ;
2015-10-14 17:52:04 +02:00
} ) ;