Skip to content

Commit ef1feeb

Browse files
committed
[css-color-4] Avoid divide by zero in sample code
1 parent 237a5d2 commit ef1feeb

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

css-color-4/conversions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ function rectangular_un_premultiply(color, alpha) {
462462
// given a premultiplied color in a rectangular orthogonal colorspace
463463
// and an alpha value
464464
// return the actual color
465+
if (alpha = 0) {
466+
return color; // avoid divide by zero
467+
}
465468
return color.map((c) => c / alpha)
466469
}
467470

@@ -482,6 +485,9 @@ function polar_un_premultiply(color, alpha, hueIndex) {
482485
// the hueIndex says which entry in the color array corresponds to hue angle
483486
// for example, in OKLCH it would be 2
484487
// while in HSL it would be 0
488+
if (alpha = 0) {
489+
return color; // avoid divide by zero
490+
}
485491
return color.map((c, i) => c / (hueIndex === i? 1 : alpha))
486492
}
487493

0 commit comments

Comments
 (0)