Skip to content

Commit 07eb827

Browse files
committed
Modified color2 to be optional in findContrast method
1 parent c0594b8 commit 07eb827

9 files changed

Lines changed: 63 additions & 57 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,18 @@ const distance = Color.dist(color1, color2);
454454
Find an optimally contrasting color for another color.
455455

456456
- `color1` is a *Color* object.
457-
- `color2` is a *Color* object.
457+
- `color2` is a *Color* object, and will default to *null*.
458458
- `minContrast` is a number between *1* and *21* indicating the minimum valid contrast, and will default to *4.5*.
459459
- `stepSize` is a number between *0* and *1* indicating the amount to darken/lighten the color on each iteration, and will default to *0.01*.
460460

461461
```javascript
462462
const contrastColor = Color.findContrast(color1, color2, minContrast, stepSize);
463463
```
464464

465+
If `color2` value is *null*, a clone of `color1` will be used instead.
466+
467+
This method will tint/shade `color2` until it meets a minimum contrast threshold with `color1`, then the new color will be returned. If no valid contrast value can be found, this method will return *null* instead.
468+
465469
**Mix**
466470

467471
Create a new *Color* by mixing two colors together by a specified amount.

dist/frost-color.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostColor v2.0.5
2+
* FrostColor v2.0.6
33
* https://github.com/elusivecodes/FrostColor
44
*/
55
(function(global, factory) {
@@ -944,31 +944,28 @@
944944
/**
945945
* Find an optimally contrasting color for another color.
946946
* @param {Color} color1 The first Color.
947-
* @param {Color} color2 The second Color.
947+
* @param {Color} [color2] The second Color.
948948
* @param {number} [minContrast=4.5] The minimum contrast.
949949
* @param {number} [stepSize=1] The step size.
950950
* @returns {Color} The new Color.
951951
*/
952-
findContrast(color1, color2, minContrast = 4.5, stepSize = 0.01) {
953-
const tempColor = this.fromString(color2.toString());
954-
955-
if (this.contrast(color1, tempColor) >= minContrast) {
956-
return tempColor;
952+
findContrast(color1, color2 = null, minContrast = 4.5, stepSize = 0.01) {
953+
if (!color2) {
954+
color2 = color1.clone();
957955
}
958956

959-
let offset = stepSize;
960-
while (offset <= 1) {
961-
const tempColor1 = tempColor.clone().tint(offset);
962-
if (this.contrast(color1, tempColor1) >= minContrast) {
963-
return tempColor1;
964-
}
957+
if (this.contrast(color1, color2) >= minContrast) {
958+
return color2;
959+
}
965960

966-
const tempColor2 = tempColor.clone().shade(offset);
967-
if (this.contrast(color1, tempColor2) >= minContrast) {
968-
return tempColor2;
961+
const methods = ['tint', 'shade'];
962+
for (let i = stepSize; i <= 1; i += stepSize) {
963+
for (const method of methods) {
964+
const tempColor = color2.clone()[method](i);
965+
if (this.contrast(color1, tempColor) >= minContrast) {
966+
return tempColor;
967+
}
969968
}
970-
971-
offset += stepSize;
972969
}
973970

974971
return null;

dist/frost-color.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frostcolor",
3-
"version": "2.0.5",
3+
"version": "2.0.6",
44
"description": "FrostColor is a free, open-source color manipulation library for JavaScript.",
55
"keywords": [
66
"color",

src/Color/static/utility.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,28 @@ Object.assign(Color, {
3333
/**
3434
* Find an optimally contrasting color for another color.
3535
* @param {Color} color1 The first Color.
36-
* @param {Color} color2 The second Color.
36+
* @param {Color} [color2] The second Color.
3737
* @param {number} [minContrast=4.5] The minimum contrast.
3838
* @param {number} [stepSize=1] The step size.
3939
* @returns {Color} The new Color.
4040
*/
41-
findContrast(color1, color2, minContrast = 4.5, stepSize = 0.01) {
42-
const tempColor = this.fromString(color2.toString());
43-
44-
if (this.contrast(color1, tempColor) >= minContrast) {
45-
return tempColor;
41+
findContrast(color1, color2 = null, minContrast = 4.5, stepSize = 0.01) {
42+
if (!color2) {
43+
color2 = color1.clone();
4644
}
4745

48-
let offset = stepSize;
49-
while (offset <= 1) {
50-
const tempColor1 = tempColor.clone().tint(offset);
51-
if (this.contrast(color1, tempColor1) >= minContrast) {
52-
return tempColor1;
53-
}
46+
if (this.contrast(color1, color2) >= minContrast) {
47+
return color2;
48+
}
5449

55-
const tempColor2 = tempColor.clone().shade(offset);
56-
if (this.contrast(color1, tempColor2) >= minContrast) {
57-
return tempColor2;
50+
const methods = ['tint', 'shade'];
51+
for (let i = stepSize; i <= 1; i += stepSize) {
52+
for (const method of methods) {
53+
const tempColor = color2.clone()[method](i);
54+
if (this.contrast(color1, tempColor) >= minContrast) {
55+
return tempColor;
56+
}
5857
}
59-
60-
offset += stepSize;
6158
}
6259

6360
return null;

src/wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostColor v2.0.5
2+
* FrostColor v2.0.6
33
* https://github.com/elusivecodes/FrostColor
44
*/
55
(function(global, factory) {

test.html

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/Color/static.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ describe('Color Static', function() {
4848
});
4949

5050
describe('#findContrast', function() {
51+
it('returns a minimally contrasting color', function() {
52+
const color1 = new Color(203, 213, 255);
53+
const color2 = Color.findContrast(color1);
54+
assert.strictEqual(
55+
color2.toString(),
56+
'#575c6e'
57+
);
58+
assert.ok(
59+
color2 instanceof Color
60+
);
61+
});
62+
5163
it('returns a minimally contrasting color between two colors', function() {
5264
const color1 = new Color(203, 213, 255);
5365
const color2 = new Color(122, 143, 255);
@@ -96,7 +108,7 @@ describe('Color Static', function() {
96108
'#4c599e'
97109
);
98110
assert.ok(
99-
color3 instanceof Color
111+
color3 instanceof ColorImmutable
100112
);
101113
});
102114
});

test/ColorImmutable/static.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ describe('ColorImmutable Static', function() {
4848
});
4949

5050
describe('#findContrast', function() {
51+
it('returns a minimally contrasting color', function() {
52+
const color1 = new ColorImmutable(203, 213, 255);
53+
const color2 = ColorImmutable.findContrast(color1);
54+
assert.strictEqual(
55+
color2.toString(),
56+
'#575c6e'
57+
);
58+
assert.ok(
59+
color2 instanceof ColorImmutable
60+
);
61+
});
62+
5163
it('returns a minimally contrasting color between two colors', function() {
5264
const color1 = new ColorImmutable(203, 213, 255);
5365
const color2 = new ColorImmutable(122, 143, 255);
@@ -96,7 +108,7 @@ describe('ColorImmutable Static', function() {
96108
'#4c599e'
97109
);
98110
assert.ok(
99-
color3 instanceof ColorImmutable
111+
color3 instanceof Color
100112
);
101113
});
102114
});

0 commit comments

Comments
 (0)