2021-10-14 06:26:26 +08:00
import fs from "fs" ;
import path from "path" ;
import { PNG , PNGWithMetadata } from "pngjs" ;
2021-10-22 22:52:03 +08:00
import { log , LogStyle } from "./logging" ;
2021-10-14 06:26:26 +08:00
2021-10-22 22:52:03 +08:00
export const assert = ( condition : boolean , onFailMessage : string ) = > { if ( ! condition ) { log ( LogStyle . Failure , onFailMessage ) ; process . exit ( 0 ) ; } }
2021-10-14 06:26:26 +08:00
export function isDirSetup ( relativePath : string , jarAssetDir : string ) {
const dir = path . join ( __dirname , relativePath )
if ( fs . existsSync ( dir ) ) {
if ( fs . readdirSync ( dir ) . length > 0 ) {
return true ;
}
} else {
fs . mkdirSync ( dir ) ;
}
2021-10-22 22:52:03 +08:00
log ( LogStyle . Warning , ` Copy the contents of .minecraft/versions/<version>/<version>.jar/ ${ jarAssetDir } from a Minecraft game files into ${ relativePath } ` ) ;
2021-10-14 06:26:26 +08:00
return false ;
}
2021-10-22 22:52:03 +08:00
export function getAverageColour ( image : PNG ) {
2021-10-14 06:26:26 +08:00
let r = 0 , g = 0 , b = 0 ;
for ( let x = 0 ; x < image . width ; ++ x ) {
for ( let y = 0 ; y < image . height ; ++ y ) {
const index = 4 * ( image . width * y + x ) ;
const rgba = image . data . slice ( index , index + 4 ) ;
r += rgba [ 0 ] ;
g += rgba [ 1 ] ;
b += rgba [ 2 ] ;
}
}
const numPixels = image . width * image . height ;
return { r : r / ( 255 * numPixels ) , g : g / ( 255 * numPixels ) , b : b / ( 255 * numPixels ) } ;
}