|
1 | 1 | /** |
2 | | - * FrostColor v2.0.5 |
| 2 | + * FrostColor v2.0.6 |
3 | 3 | * https://github.com/elusivecodes/FrostColor |
4 | 4 | */ |
5 | 5 | (function(global, factory) { |
|
944 | 944 | /** |
945 | 945 | * Find an optimally contrasting color for another color. |
946 | 946 | * @param {Color} color1 The first Color. |
947 | | - * @param {Color} color2 The second Color. |
| 947 | + * @param {Color} [color2] The second Color. |
948 | 948 | * @param {number} [minContrast=4.5] The minimum contrast. |
949 | 949 | * @param {number} [stepSize=1] The step size. |
950 | 950 | * @returns {Color} The new Color. |
951 | 951 | */ |
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(); |
957 | 955 | } |
958 | 956 |
|
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 | + } |
965 | 960 |
|
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 | + } |
969 | 968 | } |
970 | | - |
971 | | - offset += stepSize; |
972 | 969 | } |
973 | 970 |
|
974 | 971 | return null; |
|
0 commit comments