Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/compiler-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const compilerCommand = (() => {
process.execPath,
// This is a fallback which is required indirectly through
// sass-embedded-all-unknown.
// eslint-disable-next-line n/no-extraneous-require

p.join(p.dirname(require.resolve('sass')), 'sass.js'),
];
} catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/legacy/value/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ export class LegacyColor extends LegacyValueBase<SassColor> {
}

getR(): number {
return this.inner.red;
return Math.round(this.inner.channel('red'));
}

setR(value: number): void {
this.inner = this.inner.change({red: clamp(value, 0, 255)});
}

getG(): number {
return this.inner.green;
return Math.round(this.inner.channel('green'));
}

setG(value: number): void {
this.inner = this.inner.change({green: clamp(value, 0, 255)});
}

getB(): number {
return this.inner.blue;
return Math.round(this.inner.channel('blue'));
}

setB(value: number): void {
Expand Down
257 changes: 23 additions & 234 deletions lib/src/value/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// https://opensource.org/licenses/MIT.

import {Value} from './index';
import {deprecations, warnForHostSideDeprecation} from '../deprecations';
import {valueError} from '../utils';
import {
fuzzyAssertInRange,
Expand Down Expand Up @@ -293,66 +292,6 @@ function isNumberOrNull(val: undefined | null | number): val is number | null {
return val === null || typeof val === 'number';
}

/**
* Emit deprecation warnings when legacy color spaces set `alpha` or channel
* values to `null` without explicitly setting the `space`.
*/
function checkChangeDeprecations(
options: {
[key in ChannelName]?: number | null;
},
channels: ChannelName[],
): void {
if (options.alpha === null) emitNullAlphaDeprecation();
for (const channel of channels) {
if (options[channel] === null) emitColor4ApiChangeNullDeprecation(channel);
}
}

/** Warn users about legacy color channel getters. */
function emitColor4ApiGetterDeprecation(name: string): void {
warnForHostSideDeprecation(
`\`${name}\` is deprecated, use \`channel\` instead.` +
'\n' +
'More info: https://sass-lang.com/d/color-4-api',
deprecations['color-4-api'],
);
}

/**
* Warn users about changing channels not in the current color space without
* explicitly setting `space`.
*/
function emitColor4ApiChangeSpaceDeprecation(): void {
warnForHostSideDeprecation(
"Changing a channel not in this color's space without explicitly " +
'specifying the `space` option is deprecated.' +
'\n' +
'More info: https://sass-lang.com/d/color-4-api',
deprecations['color-4-api'],
);
}

/** Warn users about `null` channel values without setting `space`. */
function emitColor4ApiChangeNullDeprecation(channel: string): void {
warnForHostSideDeprecation(
`Passing \`${channel}: null\` without setting \`space\` is deprecated.` +
'\n' +
'More info: https://sass-lang.com/d/color-4-api',
deprecations['color-4-api'],
);
}

/** Warn users about null-alpha deprecation. */
function emitNullAlphaDeprecation(): void {
warnForHostSideDeprecation(
'Passing `alpha: null` without setting `space` is deprecated.' +
'\n' +
'More info: https://sass-lang.com/d/null-alpha',
deprecations['null-alpha'],
);
}

/**
* Determines whether the options passed to the Constructor include an existing
* ColorJS color object.
Expand Down Expand Up @@ -456,7 +395,6 @@ export class SassColor extends Value {
if (space === 'rgb') this.isRgb = true;
let alpha: number;
if (options.alpha === null) {
if (!options.space) emitNullAlphaDeprecation();
alpha = NaN;
} else if (options.alpha === undefined) {
alpha = 1;
Expand Down Expand Up @@ -629,89 +567,6 @@ export class SassColor extends Value {
return List(coords.map(NaNtoZero));
}

/**
* This color's red channel in the RGB color space, between `0` and `255`.
*
* @deprecated Use {@link channel} instead.
*/
get red(): number {
emitColor4ApiGetterDeprecation('red');
const val = NaNtoZero(coordToRgb(this.color.srgb.red));
return fuzzyRound(val);
}

/**
* This color's green channel in the RGB color space, between `0` and `255`.
*
* @deprecated Use {@link channel} instead.
*/
get green(): number {
emitColor4ApiGetterDeprecation('green');
const val = NaNtoZero(coordToRgb(this.color.srgb.green));
return fuzzyRound(val);
}

/**
* This color's blue channel in the RGB color space, between `0` and `255`.
*
* @deprecated Use {@link channel} instead.
*/
get blue(): number {
emitColor4ApiGetterDeprecation('blue');
const val = NaNtoZero(coordToRgb(this.color.srgb.blue));
return fuzzyRound(val);
}

/**
* This color's hue in the HSL color space, between `0` and `360`.
*
* @deprecated Use {@link channel} instead.
*/
get hue(): number {
emitColor4ApiGetterDeprecation('hue');
return NaNtoZero(this.color.hsl.hue);
}

/**
* This color's saturation in the HSL color space, between `0` and `100`.
*
* @deprecated Use {@link channel} instead.
*/
get saturation(): number {
emitColor4ApiGetterDeprecation('saturation');
return NaNtoZero(this.color.hsl.saturation);
}

/**
* This color's lightness in the HSL color space, between `0` and `100`.
*
* @deprecated Use {@link channel} instead.
*/
get lightness(): number {
emitColor4ApiGetterDeprecation('lightness');
return NaNtoZero(this.color.hsl.lightness);
}

/**
* This color's whiteness in the HWB color space, between `0` and `100`.
*
* @deprecated Use {@link channel} instead.
*/
get whiteness(): number {
emitColor4ApiGetterDeprecation('whiteness');
return NaNtoZero(this.color.hwb.whiteness);
}

/**
* This color's blackness in the HWB color space, between `0` and `100`.
*
* @deprecated Use {@link channel} instead.
*/
get blackness(): number {
emitColor4ApiGetterDeprecation('blackness');
return NaNtoZero(this.color.hwb.blackness);
}

assertColor(): SassColor {
return this;
}
Expand Down Expand Up @@ -913,40 +768,13 @@ export class SassColor extends Value {
});
}

/** Legacy determination of color space by option channels. */
private getLegacyChangeSpace(options: ConstructorOptions): KnownColorSpace {
let space: KnownColorSpace | undefined;
if (
isNumberOrNull(options.whiteness) ||
isNumberOrNull(options.blackness) ||
(this.space === 'hwb' && isNumberOrNull(options.hue))
) {
space = 'hwb';
} else if (
isNumberOrNull(options.hue) ||
isNumberOrNull(options.saturation) ||
isNumberOrNull(options.lightness)
) {
space = 'hsl';
} else if (
isNumberOrNull(options.red) ||
isNumberOrNull(options.green) ||
isNumberOrNull(options.blue)
) {
space = 'rgb';
}
if (space !== this.space) emitColor4ApiChangeSpaceDeprecation();
return space ?? this.space;
}

/**
* Returns a new SassColor in the given `space` that's the result of changing
* one or more of this color's channels.
*/
private getChangedColor(
options: ConstructorOptions,
space: KnownColorSpace,
spaceSetExplicitly: boolean,
): SassColor {
const color = this.toSpace(space);
function getChangedValue(channel: ChannelName): number | null {
Expand All @@ -956,64 +784,31 @@ export class SassColor extends Value {

switch (space) {
case 'hsl':
if (spaceSetExplicitly) {
return new SassColor({
hue: getChangedValue('hue'),
saturation: getChangedValue('saturation'),
lightness: getChangedValue('lightness'),
alpha: getChangedValue('alpha'),
space,
});
} else {
checkChangeDeprecations(options, ['hue', 'saturation', 'lightness']);
return new SassColor({
hue: options.hue ?? color.channel('hue'),
saturation: options.saturation ?? color.channel('saturation'),
lightness: options.lightness ?? color.channel('lightness'),
alpha: options.alpha ?? color.channel('alpha'),
space,
});
}
return new SassColor({
hue: getChangedValue('hue'),
saturation: getChangedValue('saturation'),
lightness: getChangedValue('lightness'),
alpha: getChangedValue('alpha'),
space,
});

case 'hwb':
if (spaceSetExplicitly) {
return new SassColor({
hue: getChangedValue('hue'),
whiteness: getChangedValue('whiteness'),
blackness: getChangedValue('blackness'),
alpha: getChangedValue('alpha'),
space,
});
} else {
checkChangeDeprecations(options, ['hue', 'whiteness', 'blackness']);
return new SassColor({
hue: options.hue ?? color.channel('hue'),
whiteness: options.whiteness ?? color.channel('whiteness'),
blackness: options.blackness ?? color.channel('blackness'),
alpha: options.alpha ?? color.channel('alpha'),
space,
});
}
return new SassColor({
hue: getChangedValue('hue'),
whiteness: getChangedValue('whiteness'),
blackness: getChangedValue('blackness'),
alpha: getChangedValue('alpha'),
space,
});

case 'rgb':
if (spaceSetExplicitly) {
return new SassColor({
red: getChangedValue('red'),
green: getChangedValue('green'),
blue: getChangedValue('blue'),
alpha: getChangedValue('alpha'),
space,
});
} else {
checkChangeDeprecations(options, ['red', 'green', 'blue']);
return new SassColor({
red: options.red ?? color.channel('red'),
green: options.green ?? color.channel('green'),
blue: options.blue ?? color.channel('blue'),
alpha: options.alpha ?? color.channel('alpha'),
space,
});
}
return new SassColor({
red: getChangedValue('red'),
green: getChangedValue('green'),
blue: getChangedValue('blue'),
alpha: getChangedValue('alpha'),
space,
});

case 'lab':
case 'oklab':
Expand Down Expand Up @@ -1110,11 +905,7 @@ export class SassColor extends Value {
},
): SassColor;
change(options: ConstructorOptions): SassColor {
const spaceSetExplicitly = !!options.space;
let space = options.space ?? this.space;
if (this.isLegacy && !spaceSetExplicitly) {
space = this.getLegacyChangeSpace(options);
}
const space = options.space ?? this.space;

// Validate channel values
const keys = Object.keys(options).filter(
Expand All @@ -1127,9 +918,7 @@ export class SassColor extends Value {
fuzzyAssertInRange(options.alpha, 0, 1, 'alpha');
}

return this.getChangedColor(options, space, spaceSetExplicitly).toSpace(
this.space,
);
return this.getChangedColor(options, space).toSpace(this.space);
}

equals(other: Value): boolean {
Expand Down