@@ -7,20 +7,42 @@ const forbiddenChars = /\s/g;
77
88function InitGradient ( ) {
99 const grad = tinygradient . apply ( this , arguments ) ;
10- return ( str , opts ) => applyGradient ( str ? str . toString ( ) : '' , grad , opts ) ;
10+ const ret = ( str , opts ) => applyGradient ( str ? str . toString ( ) : '' , grad , opts ) ;
11+ ret . multiline = ( str , opts ) => multilineGradient ( str ? str . toString ( ) : '' , grad , opts ) ;
12+ return ret ;
1113}
1214
15+ const getColors = ( gradient , options , count ) => options . interpolation . toLowerCase ( ) === 'hsv' ?
16+ gradient . hsv ( count , options . hsvSpin . toLowerCase ( ) ) : gradient . rgb ( count ) ;
17+
1318function applyGradient ( str , gradient , opts ) {
1419 const options = validateOptions ( opts ) ;
1520 const colorsCount = Math . max ( str . replace ( forbiddenChars , '' ) . length , gradient . stops . length ) ;
16- const colors = options . interpolation . toLowerCase ( ) === 'hsv' ? gradient . hsv ( colorsCount , options . hsvSpin . toLowerCase ( ) ) : gradient . rgb ( colorsCount ) ;
21+ const colors = getColors ( gradient , options , colorsCount ) ;
1722 let result = '' ;
1823 for ( const s of str ) {
1924 result += s . match ( forbiddenChars ) ? s : chalk . hex ( colors . shift ( ) . toHex ( ) ) ( s ) ;
2025 }
2126 return result ;
2227}
2328
29+ function multilineGradient ( str , gradient , opts ) {
30+ const options = validateOptions ( opts ) ;
31+ const lines = str . split ( '\n' ) ;
32+ const maxLength = Math . max ( gradient . stops . length , ...( lines . map ( l => l . length ) ) ) ;
33+ const colors = getColors ( gradient , options , maxLength ) ;
34+ const results = [ ] ;
35+ for ( const line of lines ) {
36+ const lineColors = colors . slice ( 0 ) ;
37+ let lineResult = '' ;
38+ for ( const l of line ) {
39+ lineResult += chalk . hex ( lineColors . shift ( ) . toHex ( ) ) ( l ) ;
40+ }
41+ results . push ( lineResult ) ;
42+ }
43+ return results . join ( '\n' ) ;
44+ }
45+
2446function validateOptions ( opts ) {
2547 const options = Object . assign ( { interpolation : 'rgb' , hsvSpin : 'short' } , opts ) ;
2648 if ( opts !== undefined && typeof opts !== 'object' ) {
@@ -37,18 +59,24 @@ function validateOptions(opts) {
3759 return options ;
3860}
3961
40- module . exports = InitGradient ;
62+ const aliases = {
63+ atlas : { colors : [ '#feac5e' , '#c779d0' , '#4bc0c8' ] , options : { } } ,
64+ cristal : { colors : [ '#bdfff3' , '#4ac29a' ] , options : { } } ,
65+ teen : { colors : [ '#77a1d3' , '#79cbca' , '#e684ae' ] , options : { } } ,
66+ mind : { colors : [ '#473b7b' , '#3584a7' , '#30d2be' ] , options : { } } ,
67+ morning : { colors : [ '#ff5f6d' , '#ffc371' ] , options : { interpolation : 'hsv' } } ,
68+ vice : { colors : [ '#5ee7df' , '#b490ca' ] , options : { interpolation : 'hsv' } } ,
69+ passion : { colors : [ '#f43b47' , '#453a94' ] , options : { } } ,
70+ fruit : { colors : [ '#ff4e50' , '#f9d423' ] , options : { } } ,
71+ instagram : { colors : [ '#833ab4' , '#fd1d1d' , '#fcb045' ] , options : { } } ,
72+ retro : { colors : [ '#3f51b1' , '#5a55ae' , '#7b5fac' , '#8f6aae' , '#a86aa4' , '#cc6b8e' , '#f18271' , '#f3a469' , '#f7c978' ] , options : { } } ,
73+ summer : { colors : [ '#fdbb2d' , '#22c1c3' ] , options : { } } ,
74+ rainbow : { colors : [ '#ff0000' , '#ff0100' ] , options : { interpolation : 'hsv' , hsvSpin : 'long' } } ,
75+ pastel : { colors : [ '#74ebd5' , '#74ecd5' ] , options : { interpolation : 'hsv' , hsvSpin : 'long' } }
76+ } ;
4177
42- module . exports . atlas = str => new InitGradient ( '#feac5e' , '#c779d0' , '#4bc0c8' ) ( str ) ;
43- module . exports . cristal = str => new InitGradient ( '#bdfff3' , '#4ac29a' ) ( str ) ;
44- module . exports . teen = str => new InitGradient ( '#77a1d3' , '#79cbca' , '#e684ae' ) ( str ) ;
45- module . exports . mind = str => new InitGradient ( '#473b7b' , '#3584a7' , '#30d2be' ) ( str ) ;
46- module . exports . morning = str => new InitGradient ( '#ff5f6d' , '#ffc371' ) ( str , { interpolation : 'hsv' } ) ;
47- module . exports . vice = str => new InitGradient ( '#5ee7df' , '#b490ca' ) ( str , { interpolation : 'hsv' } ) ;
48- module . exports . passion = str => new InitGradient ( '#f43b47' , '#453a94' ) ( str ) ;
49- module . exports . fruit = str => new InitGradient ( '#ff4e50' , '#f9d423' ) ( str ) ;
50- module . exports . instagram = str => new InitGradient ( '#833ab4' , '#fd1d1d' , '#fcb045' ) ( str ) ;
51- module . exports . retro = str => new InitGradient ( '#3f51b1' , '#5a55ae' , '#7b5fac' , '#8f6aae' , '#a86aa4' , '#cc6b8e' , '#f18271' , '#f3a469' , '#f7c978' ) ( str ) ;
52- module . exports . summer = str => new InitGradient ( '#fdbb2d' , '#22c1c3' ) ( str ) ;
53- module . exports . rainbow = str => new InitGradient ( '#ff0000' , '#ff0100' ) ( str , { interpolation : 'hsv' , hsvSpin : 'long' } ) ;
54- module . exports . pastel = str => new InitGradient ( '#74ebd5' , '#74ecd5' ) ( str , { interpolation : 'hsv' , hsvSpin : 'long' } ) ;
78+ module . exports = InitGradient ;
79+ for ( const a in aliases ) { // eslint-disable-line guard-for-in
80+ module . exports [ a ] = str => new InitGradient ( aliases [ a ] . colors ) ( str , aliases [ a ] . options ) ;
81+ module . exports [ a ] . multiline = str => new InitGradient ( aliases [ a ] . colors ) . multiline ( str , aliases [ a ] . options ) ;
82+ }
0 commit comments