book-source 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/documentation.md +2 -2
- package/lib/StructuredDocument.js +124 -139
- package/lib/Style.js +7 -1
- package/lib/Theme.js +3 -2
- package/lib/book-source.js +6 -28
- package/lib/documentParts.js +445 -0
- package/package.json +8 -3
- package/css/code.css +0 -120
- package/css/core.css +0 -317
- package/css/standalone.css +0 -30
- package/css/unimplemented.css +0 -65
- package/extlib/chromajs.custom.js +0 -865
- package/lib/Color.js +0 -197
- package/lib/HtmlRenderer.js +0 -618
- package/lib/Palette.js +0 -313
package/lib/Palette.js
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Book Source
|
|
3
|
-
|
|
4
|
-
Copyright (c) 2023 Cédric Ronvel
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
in the Software without restriction, including without limitation the rights
|
|
11
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
furnished to do so, subject to the following conditions:
|
|
14
|
-
|
|
15
|
-
The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
copies or substantial portions of the Software.
|
|
17
|
-
|
|
18
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
SOFTWARE.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
"use strict" ;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
//const chromajs = require( 'chroma-js' ) ;
|
|
32
|
-
|
|
33
|
-
// Custom build of chroma-js, using the fork regorxxx/chroma.js, and a customized index-ultra-light.js,
|
|
34
|
-
// dividing the size of the chroma-js lib by 4.5.
|
|
35
|
-
const chromajs = require( '../extlib/chromajs.custom.js' ) ;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const DEFAULT_COLORS = {
|
|
40
|
-
'black': '#24292e' ,
|
|
41
|
-
'white': '#ffffff' ,
|
|
42
|
-
'gray': '#74797e' ,
|
|
43
|
-
|
|
44
|
-
'red': '#e32322' ,
|
|
45
|
-
'orange': '#f18e1c' ,
|
|
46
|
-
'yellow-orange': '#fdc60b' ,
|
|
47
|
-
'yellow': '#f4e500' ,
|
|
48
|
-
'yellow-green': '#8cbb26' ,
|
|
49
|
-
'green': '#25ad28' ,
|
|
50
|
-
'blue-green': '#1bc17d' ,
|
|
51
|
-
'cyan': '#0dc0cd' ,
|
|
52
|
-
'blue': '#2a60b0' ,
|
|
53
|
-
'blue-violet': '#3b3ba2' ,
|
|
54
|
-
'violet': '#713795' ,
|
|
55
|
-
'red-violet': '#bd0a7d'
|
|
56
|
-
} ;
|
|
57
|
-
|
|
58
|
-
const DEFAULT_ALIASES = {
|
|
59
|
-
'grey': 'gray' ,
|
|
60
|
-
'orange-yellow': 'yellow-orange' ,
|
|
61
|
-
'amber': 'yellow-orange' ,
|
|
62
|
-
'gold': 'yellow-orange' ,
|
|
63
|
-
'green-yellow': 'yellow-green' ,
|
|
64
|
-
'chartreuse': 'yellow-green' ,
|
|
65
|
-
'green-blue': 'blue-green' ,
|
|
66
|
-
'turquoise': 'blue-green' ,
|
|
67
|
-
'turquoise-green': 'blue-green' ,
|
|
68
|
-
'teal': 'blue-green' ,
|
|
69
|
-
'turquoise-blue': 'cyan' ,
|
|
70
|
-
'violet-blue': 'blue-violet' ,
|
|
71
|
-
'indigo': 'blue-violet' ,
|
|
72
|
-
'purple': 'violet' ,
|
|
73
|
-
'violet-red': 'red-violet' ,
|
|
74
|
-
'magenta': 'red-violet'
|
|
75
|
-
} ;
|
|
76
|
-
|
|
77
|
-
const EXTRA_COLORS = {
|
|
78
|
-
'crimson': '#dc143c' ,
|
|
79
|
-
'vermilion': '#e34234' ,
|
|
80
|
-
'brown': '#a52a2a' ,
|
|
81
|
-
'bronze': '#cd7f32' ,
|
|
82
|
-
'coquelicot': '#ff3800' ,
|
|
83
|
-
//'flame': '#e25822' ,
|
|
84
|
-
//'salmon': '#ff8c69' ,
|
|
85
|
-
'coral-pink': '#f88379' ,
|
|
86
|
-
'see-green': '#2e8b57' ,
|
|
87
|
-
'medium-spring-green': '#00fa9a' ,
|
|
88
|
-
'olivine': '#9ab973' ,
|
|
89
|
-
'royal-blue': '#4169e1' ,
|
|
90
|
-
'purple': '#800080' ,
|
|
91
|
-
//'tyrian-purple': '#66023c' ,
|
|
92
|
-
//'purple-heart': '#69359c' ,
|
|
93
|
-
'lavender-purple': '#967bb6' ,
|
|
94
|
-
//'classic-rose' , 'light-pink': '#fbcce7' ,
|
|
95
|
-
'pink': '#ffc0cb'
|
|
96
|
-
//'lime': '#bfff00' ,
|
|
97
|
-
} ;
|
|
98
|
-
|
|
99
|
-
const EXTRA_ALIASES = {
|
|
100
|
-
'cinnabar': 'vermilion'
|
|
101
|
-
//'lemon-lime': 'lime'
|
|
102
|
-
} ;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
function Palette( params = {} ) {
|
|
107
|
-
this.colors = {} ;
|
|
108
|
-
this.aliases = Object.assign( {} , DEFAULT_ALIASES ) ;
|
|
109
|
-
|
|
110
|
-
if ( params.aliases ) { this.addAliases( params.colors ) ; }
|
|
111
|
-
|
|
112
|
-
if ( params.colors ) { this.addColors( params.colors ) ; }
|
|
113
|
-
else if ( ! params.aliases ) { this.addColors( params ) ; }
|
|
114
|
-
this.addColors( DEFAULT_COLORS , true ) ;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
module.exports = Palette ;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
Palette.prototype.addAliases = function( aliases ) {
|
|
122
|
-
if ( ! aliases || typeof aliases !== 'object' ) { return ; }
|
|
123
|
-
Object.assign( this.aliases , aliases ) ;
|
|
124
|
-
} ;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
Palette.prototype.addColors = function( colors , ifNotExists = false ) {
|
|
129
|
-
if ( ! colors || typeof colors !== 'object' ) { return ; }
|
|
130
|
-
|
|
131
|
-
for ( let name in colors ) {
|
|
132
|
-
let colorDef = colors[ name ] ;
|
|
133
|
-
if ( this.aliases[ name ] ) { name = this.aliases[ name ] ; }
|
|
134
|
-
|
|
135
|
-
if ( colorDef && ( ! ifNotExists || ! this.colors[ name ] ) ) {
|
|
136
|
-
this.colors[ name ] = chromajs( colorDef ) ;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
} ;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
Palette.prototype.has = function( colorObject ) {
|
|
144
|
-
var name = colorObject.baseName ;
|
|
145
|
-
if ( this.aliases[ name ] ) { name = this.aliases[ name ] ; }
|
|
146
|
-
return !! this.colors[ name ] ;
|
|
147
|
-
} ;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
Palette.prototype.getHex = function( colorObject ) {
|
|
152
|
-
var name = colorObject.baseName ;
|
|
153
|
-
if ( this.aliases[ name ] ) { name = this.aliases[ name ] ; }
|
|
154
|
-
|
|
155
|
-
var chromaColor = this.colors[ name ] ;
|
|
156
|
-
if ( ! chromaColor ) { return null ; }
|
|
157
|
-
|
|
158
|
-
if ( colorObject.hasModifier() ) {
|
|
159
|
-
chromaColor = Palette.adjust( chromaColor , colorObject ) ;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return chromaColor.hex() ;
|
|
163
|
-
} ;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
Palette.prototype.getRgb = function( colorObject ) {
|
|
168
|
-
var name = colorObject.baseName ;
|
|
169
|
-
if ( this.aliases[ name ] ) { name = this.aliases[ name ] ; }
|
|
170
|
-
|
|
171
|
-
var chromaColor = this.colors[ name ] ;
|
|
172
|
-
if ( ! chromaColor ) { return null ; }
|
|
173
|
-
|
|
174
|
-
if ( colorObject.hasModifier() ) {
|
|
175
|
-
chromaColor = Palette.adjust( chromaColor , colorObject ) ;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
var [ r , g , b ] = chromaColor.rgb() ;
|
|
179
|
-
return { r , g , b } ;
|
|
180
|
-
} ;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const LCH_L_STEP = 18 ;
|
|
185
|
-
const LCH_C_STEP = 18 ;
|
|
186
|
-
const LCH_C_RATE_STEP = 0.25 ;
|
|
187
|
-
|
|
188
|
-
/*
|
|
189
|
-
Chroma-js .brighten()/.darken() uses a +/- 18 increment on L of the LCH colorspace,
|
|
190
|
-
while .saturate()/.desaturate() also uses +/- 18 on C of the LCH colorspace.
|
|
191
|
-
*/
|
|
192
|
-
Palette.adjust = function( chromaColor , colorObject ) {
|
|
193
|
-
if ( colorObject.tintRate ) { chromaColor = chromajs.mix( chromaColor , '#ffffff' , colorObject.tintRate ) ; }
|
|
194
|
-
else if ( colorObject.toneRate ) { chromaColor = chromajs.mix( chromaColor , '#808080' , colorObject.toneRate ) ; }
|
|
195
|
-
else if ( colorObject.shadeRate ) { chromaColor = chromajs.mix( chromaColor , '#000000' , colorObject.shadeRate ) ; }
|
|
196
|
-
|
|
197
|
-
var lch = chromaColor.lch() ;
|
|
198
|
-
|
|
199
|
-
if ( colorObject.lightnessLevel ) { lch[ 0 ] = Math.max( 0 , lch[ 0 ] + LCH_L_STEP * colorObject.lightnessLevel ) ; }
|
|
200
|
-
if ( colorObject.saturationLevel ) { lch[ 1 ] = Math.max( 0 , lch[ 1 ] * ( 1 + LCH_C_RATE_STEP * colorObject.saturationLevel ) ) ; }
|
|
201
|
-
|
|
202
|
-
//console.error( "Initial lch:" , lch ) ;
|
|
203
|
-
chromaColor = chromajs( ... lch , 'lch' ) ;
|
|
204
|
-
|
|
205
|
-
if ( chromaColor._rgb._clipped ) {
|
|
206
|
-
//console.error( "BF clip:" , chromaColor , lch ) ;
|
|
207
|
-
chromaColor = Palette.cleanClip( chromaColor , lch ) ;
|
|
208
|
-
//console.error( "AFT clip:" , chromaColor , lch ) ;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return chromaColor ;
|
|
212
|
-
} ;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// How much we sacrifice chroma over brightness, 1=reduce both with the same factor, 2=reduce twice the chroma relative to the lightness
|
|
217
|
-
const CHROMA_FLEXIBILITY = 3 ;
|
|
218
|
-
|
|
219
|
-
/*
|
|
220
|
-
Max LCH's L is 100.
|
|
221
|
-
Max LCH's C is 134 for pure blue (120 for pure green and 105 for pure red)
|
|
222
|
-
*/
|
|
223
|
-
Palette.cleanClip = function( chromaColor , lch ) {
|
|
224
|
-
if ( ! chromaColor._rgb._clipped ) { return chromaColor ; }
|
|
225
|
-
|
|
226
|
-
var lcRatio , currentLcRatio ,
|
|
227
|
-
lchClipped = false ;
|
|
228
|
-
|
|
229
|
-
// First, non-sensical or excessive LCH values...
|
|
230
|
-
if ( lch[ 0 ] < 0 ) { lch[ 0 ] = 0 ; }
|
|
231
|
-
if ( lch[ 1 ] < 0 ) { lch[ 1 ] = 0 ; }
|
|
232
|
-
lcRatio = lch[ 0 ] / lch[ 1 ] ;
|
|
233
|
-
|
|
234
|
-
if ( lch[ 0 ] > 100 ) { lch[ 0 ] = 100 ; lchClipped = true ; }
|
|
235
|
-
if ( lch[ 1 ] > 134 ) { lch[ 1 ] = 134 ; lchClipped = true ; }
|
|
236
|
-
|
|
237
|
-
if ( lchClipped ) {
|
|
238
|
-
currentLcRatio = lch[ 0 ] / lch[ 1 ] ;
|
|
239
|
-
if ( lch[ 0 ] > 0.5 && lch[ 1 ] > 0.5 && Number.isFinite( lcRatio ) && Number.isFinite( currentLcRatio ) ) {
|
|
240
|
-
if ( currentLcRatio > lcRatio ) {
|
|
241
|
-
lch[ 0 ] = lch[ 1 ] * lcRatio ;
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
lch[ 1 ] = lch[ 0 ] / lcRatio ;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
chromaColor = chromajs( ... lch , 'lch' ) ;
|
|
248
|
-
//console.error( "After excessive pass:" , chromaColor , lch ) ;
|
|
249
|
-
if ( ! chromaColor._rgb._clipped ) { return chromaColor ; }
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
for ( let pass = 0 ; pass < 5 ; pass ++ ) {
|
|
253
|
-
let rgb = chromaColor._rgb._unclipped ;
|
|
254
|
-
let average = ( rgb[ 0 ] + rgb[ 1 ] + rgb[ 2 ] ) / 3 ;
|
|
255
|
-
let max = Math.max( rgb[ 0 ] , rgb[ 1 ] , rgb[ 2 ] ) ;
|
|
256
|
-
let min = Math.min( rgb[ 0 ] , rgb[ 1 ] , rgb[ 2 ] ) ;
|
|
257
|
-
let reverseAverage = 255 - average ;
|
|
258
|
-
let reverseMax = 255 - max ;
|
|
259
|
-
let reverseMin = 255 - min ;
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
max > 256
|
|
264
|
-
&& ( min >= - 10 || max - 255 > - min / 4 ) // <-- hacky part, see the "else" for the explanation
|
|
265
|
-
) {
|
|
266
|
-
let rgbSaturation = max - average ;
|
|
267
|
-
|
|
268
|
-
// Compute the rates and apply it, it will change both the average values (lightness) and the rgb saturation
|
|
269
|
-
let lRate = 255 / max ;
|
|
270
|
-
let cRate = lRate ** CHROMA_FLEXIBILITY ;
|
|
271
|
-
let average2 = average * lRate ;
|
|
272
|
-
let rgbSaturation2 = rgbSaturation * lRate * cRate ;
|
|
273
|
-
let max2 = average2 + rgbSaturation2 ;
|
|
274
|
-
|
|
275
|
-
// So the real rate that was applied to the max channel is
|
|
276
|
-
let lRate2 = max2 / max ;
|
|
277
|
-
|
|
278
|
-
// Correct the final rates
|
|
279
|
-
let powerCorrector = Math.log( lRate ) / Math.log( lRate2 ) ;
|
|
280
|
-
//let lRateFinal = lRate * lRate / lRate2 ;
|
|
281
|
-
let lRateFinal = lRate ** powerCorrector ;
|
|
282
|
-
let cRateFinal = lRateFinal ** CHROMA_FLEXIBILITY ;
|
|
283
|
-
|
|
284
|
-
lch[ 0 ] *= lRateFinal ;
|
|
285
|
-
lch[ 1 ] *= cRateFinal ;
|
|
286
|
-
//console.error( "Pass " + pass + ":" , { average , max , min , rgbSaturation , lRate , cRate , average2 , rgbSaturation2 , max2 , lRate2 , powerCorrector , lRateFinal , cRateFinal } ) ;
|
|
287
|
-
}
|
|
288
|
-
else if ( min < - 10 ) {
|
|
289
|
-
/*
|
|
290
|
-
Chroma-js has bugs with négative RGB values.
|
|
291
|
-
Negative values are so insane that it's impossible to reason with.
|
|
292
|
-
They grow up really quickly without any reason once the chroma exceed even slightly the limit for a specific hue.
|
|
293
|
-
|
|
294
|
-
So the only solution is a hack.
|
|
295
|
-
Just clip negative RGB values, get the resulting chroma and apply it.
|
|
296
|
-
We will also pretend being happy with values ranging from 0 to -10.
|
|
297
|
-
*/
|
|
298
|
-
|
|
299
|
-
let clipped = chromajs( ... rgb.map( e => Math.max( 1 , e ) ) ) ;
|
|
300
|
-
let c = lch[ 1 ] ;
|
|
301
|
-
let newC = chromajs( clipped ).get( 'lch.c' ) ;
|
|
302
|
-
//console.error( "Clip negative pass " + pass + ":" , clipped , { c , newC , lch: chromajs( clipped ).lch() } ) ;
|
|
303
|
-
lch[ 1 ] = newC - 0.5 ; // Let another hack, because even after getting the new C, the hue changed, and we are back to silly negative values
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
chromaColor = chromajs( ... lch , 'lch' ) ;
|
|
307
|
-
//console.error( "After pass " + pass + ":" , chromaColor , lch ) ;
|
|
308
|
-
if ( ! chromaColor._rgb._clipped ) { return chromaColor ; }
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
return chromaColor ;
|
|
312
|
-
} ;
|
|
313
|
-
|