Skip to content

Commit

Permalink
perf: applying changes to decrese bundle size to 2.2KB
Browse files Browse the repository at this point in the history
  • Loading branch information
willmendesneto committed Aug 8, 2021
1 parent b23ce1e commit d5e1f7c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Expand Up @@ -199,6 +199,7 @@ To improve readability, these type definitions were renamed

### Updated

- Minor code changes to decrese bundle size to 2.2KB 🎉
- Using `Object.assign()` to return the best match object
- Changing the color to check: from white to black

Expand Down Expand Up @@ -232,7 +233,5 @@ To improve readability, these type definitions were renamed
[3.1.2]: https://github.com/willmendesneto/hex-to-css-filter/tree/v3.1.2
[unreleased]: https://github.com/willmendesneto/hex-to-css-filter/compare/v4.0.0...HEAD
[4.0.0]: https://github.com/willmendesneto/hex-to-css-filter/tree/v4.0.0


[Unreleased]: https://github.com/willmendesneto/hex-to-css-filter/compare/v5.0.0...HEAD
[5.0.0]: https://github.com/willmendesneto/hex-to-css-filter/tree/v5.0.0
[unreleased]: https://github.com/willmendesneto/hex-to-css-filter/compare/v5.0.0...HEAD
[5.0.0]: https://github.com/willmendesneto/hex-to-css-filter/tree/v5.0.0
29 changes: 16 additions & 13 deletions src/hex-to-css-filter.ts
Expand Up @@ -99,22 +99,25 @@ const hexToCSSFilter = (colorValue: string, opts: HexToCssConfiguration = {}): H
throw new Error(`Color value should be in HEX format. ${error}`);
}

const defaultHexToCssConfiguration = {
acceptanceLossPercentage: 5,
maxChecks: 30,
forceFilterRecalculation: false,
};

const HexToCssConfiguration: HexToCssConfiguration = Object.assign({}, defaultHexToCssConfiguration, opts);

const solver = new Solver(color, HexToCssConfiguration);
results[colorValue] = Object.assign({}, solver.solve(), {
const solver = new Solver(
color,
Object.assign(
{},
// `HexToCssConfiguration` Defaults
{
acceptanceLossPercentage: 5,
maxChecks: 30,
forceFilterRecalculation: false,
},
opts,
) as HexToCssConfiguration,
);

return (results[colorValue] = Object.assign({}, solver.solve(), {
hex: colorValue,
rgb: [red, green, blue],
cache: false,
}) as HexToCssResult;

return results[colorValue] as HexToCssResult;
}) as HexToCssResult);
};

export const clearCache = (key?: string): void => {
Expand Down
17 changes: 8 additions & 9 deletions src/solver.ts
@@ -1,14 +1,13 @@
import Color from './color';
import { HexToCssConfiguration } from './hex-to-css-filter';

type FilterValuesArray = [number, number, number, number, number, number];
interface SPSAPayload {
/** How many times the script was called to solve the color */
called?: number;
/** Percentage loss value for the generated filter */
loss: number;
/** Percentage loss per each color type organized in RGB: red, green, blue, h, s, l. */
values: FilterValuesArray;
values: [number, number, number, number, number, number];
}

export default class Solver {
Expand Down Expand Up @@ -76,7 +75,7 @@ export default class Solver {
let best = { loss: Infinity };
let counter = 0;
while (best.loss > this.options.acceptanceLossPercentage) {
const initialFilterValues: FilterValuesArray = [50, 20, 3750, 50, 100, 100];
const initialFilterValues: SPSAPayload['values'] = [50, 20, 3750, 50, 100, 100];
const result: SPSAPayload = this.spsa({
A,
a,
Expand Down Expand Up @@ -174,7 +173,7 @@ export default class Solver {
A: number;
a: number[];
c: number;
values: FilterValuesArray;
values: SPSAPayload['values'];
maxTriesInLoop: number;
called?: number;
}): SPSAPayload {
Expand All @@ -184,9 +183,9 @@ export default class Solver {
let best = null;
let bestLoss = Infinity;

const deltas = new Array(6) as FilterValuesArray;
const highArgs = new Array(6) as FilterValuesArray;
const lowArgs = new Array(6) as FilterValuesArray;
const deltas = new Array(6) as SPSAPayload['values'];
const highArgs = new Array(6) as SPSAPayload['values'];
const lowArgs = new Array(6) as SPSAPayload['values'];

// Size of all CSS filters to be applied to get the correct color
const filtersToBeAppliedSize = 6;
Expand Down Expand Up @@ -220,11 +219,11 @@ export default class Solver {
* Checks how much is the loss for the filter in RGB and HSL colors
*
* @private
* @param {FilterValuesArray} filters
* @param {SPSAPayload['values']} filters
* @returns {number}
* @memberof Solver
*/
private loss(filters: FilterValuesArray): number {
private loss(filters: SPSAPayload['values']): number {
// Argument as an Array of percentages.
const color = this.reusedColor;

Expand Down

0 comments on commit d5e1f7c

Please sign in to comment.