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
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/config@3.1.2/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/scale-domain-no-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'chroma-js': minor
---

scale.domain now returns the original domain array when called with no arguments
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
lint-and-test:
runs-on: ubuntu-latest
env:
CI: true

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up pnpm
uses: pnpm/action-setup@v4
with:
version: 10.24.0

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Lint
run: pnpm lint

- name: Test
run: pnpm test -- --run
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
chroma.js - JavaScript library for color conversions

Copyright (c) 2011-2024, Gregor Aisch
Copyright (c) 2011-2025, Gregor Aisch
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"main": "./index.js",
"scripts": {
"prepublishOnly": "npm test -- --run && node .bin/update-version.cjs && npm run build",
"prepublishOnly": "npm test -- --run && node .bin/update-version.cjs && npm run build && npm run docs",
"build": "rollup -c && cross-env DEV=1 rollup -c ",
"docs": "cd docs && make",
"docs-preview": "cd docs && make && make preview",
Expand Down
18 changes: 11 additions & 7 deletions src/generator/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (colors) {
let _nacol = chroma('#ccc');
let _spread = 0;
// const _fixed = false;
let _positions = [0, 1];
let _domain = [0, 1];
let _pos = [];
let _padding = [0, 0];
Expand Down Expand Up @@ -180,9 +181,9 @@ export default function (colors) {
if (classes != null) {
if (type(classes) === 'array') {
_classes = classes;
_domain = [classes[0], classes[classes.length - 1]];
_positions = [classes[0], classes[classes.length - 1]];
} else {
const d = chroma.analyze(_domain);
const d = chroma.analyze(_positions);
if (classes === 0) {
_classes = [d.min, d.max];
} else {
Expand All @@ -196,8 +197,11 @@ export default function (colors) {

f.domain = function (domain) {
if (!arguments.length) {
return _pos.map(p => _min + p * (_max - _min));;
// return original domain
return _domain;
}
// store original domain so we can return it later
_domain = domain.slice(0);
_min = domain[0];
_max = domain[domain.length - 1];
_pos = [];
Expand Down Expand Up @@ -228,7 +232,7 @@ export default function (colors) {
}
}
}
_domain = [_min, _max];
_positions = [_min, _max];
return f;
};

Expand Down Expand Up @@ -324,8 +328,8 @@ export default function (colors) {
} else if (numColors === 1) {
result = [f(0.5)];
} else if (numColors > 1) {
const dm = _domain[0];
const dd = _domain[1] - dm;
const dm = _positions[0];
const dd = _positions[1] - dm;
result = __range__(0, numColors, false).map((i) =>
f(dm + (i / (numColors - 1)) * dd)
);
Expand All @@ -342,7 +346,7 @@ export default function (colors) {
samples.push((_classes[i - 1] + _classes[i]) * 0.5);
}
} else {
samples = _domain;
samples = _positions;
}
result = samples.map((v) => f(v));
}
Expand Down
11 changes: 5 additions & 6 deletions test/scales.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,21 @@ describe('Some tests for scale()', () => {

describe('multi-stop domain with matching ramp stops', () => {
const f = scale(['yellow', 'red', 'black']).domain([0, 25, 100]);

it('returns the original domain positions', () => {
expect(f.domain()).toEqual([0, 25, 100]);
});

it('maps first stop', () => {
expect(f(0).hex()).toBe('#ffff00');
});

it('maps mid stop', () => {
expect(f(25).hex()).toBe('#ff8080');
expect(f(25).hex()).toBe('#ff0000');
});

it('maps last stop', () => {
expect(f(100).hex()).toBe('#000000');
});
});

});