* @function LevelRenderer.Tool.Color.prototype.mix * @description 简单两种颜色混合 * @param {string} color1 - 第一种颜色。 * @param {string} color2 - 第二种颜色。 * @param {number} weight - 混合权重[0-1]。 * @returns {string} 结果色。rgb(r,g,b)或rgba(r,g,b,a)
(color1, color2, weight)
| 696 | * @returns {string} 结果色。rgb(r,g,b)或rgba(r,g,b,a) |
| 697 | */ |
| 698 | mix(color1, color2, weight) { |
| 699 | if (!this.isCalculableColor(color1) || !this.isCalculableColor(color2)) { |
| 700 | return color1; |
| 701 | } |
| 702 | |
| 703 | if (typeof weight === 'undefined') { |
| 704 | weight = 0.5; |
| 705 | } |
| 706 | weight = 1 - this.adjust(weight, [0, 1]); |
| 707 | |
| 708 | var w = weight * 2 - 1; |
| 709 | var data1 = this.getData(this.toRGBA(color1)); |
| 710 | var data2 = this.getData(this.toRGBA(color2)); |
| 711 | |
| 712 | var d = data1[3] - data2[3]; |
| 713 | |
| 714 | var weight1 = (((w * d === -1) ? w : (w + d) / (1 + w * d)) + 1) / 2; |
| 715 | var weight2 = 1 - weight1; |
| 716 | |
| 717 | var data = []; |
| 718 | |
| 719 | for (var i = 0; i < 3; i++) { |
| 720 | data[i] = data1[i] * weight1 + data2[i] * weight2; |
| 721 | } |
| 722 | |
| 723 | var alpha = data1[3] * weight + data2[3] * (1 - weight); |
| 724 | alpha = Math.max(0, Math.min(1, alpha)); |
| 725 | |
| 726 | if (data1[3] === 1 && data2[3] === 1) {// 不考虑透明度 |
| 727 | return this.toColor(data, 'rgb'); |
| 728 | } |
| 729 | data[3] = alpha; |
| 730 | return this.toColor(data, 'rgba'); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * @function LevelRenderer.Tool.Color.prototype.random |