// The module
(function() {
angular.module('colorized', [])
.service('$colors', function() {
this.lighten = function(src, percent) {
var src = normalizeColor(src);
if (!percent) return src;
var srcRGB = colorAsArray(src),
// you may want to change it to keep a different
// range, for example, the range between the actual
// collor and the full white collor, it's up to you
lightFactor = (255 * percent)/100,
newRGB = {
r: limited(srcRGB.r + lightFactor, 255),
g: limited(srcRGB.g + lightFactor, 255),
b: limited(srcRGB.b + lightFactor, 255),
};
return [
padRGBDigit(newRGB.r.toString(16)),
padRGBDigit(newRGB.g.toString(16)),
padRGBDigit(newRGB.b.toString(16))
].join('');
}
function normalizeColor(color) {
if (color == undefined) color = '000000';
if (color[0] == '#') color = color.substring(1);
return color;
}
function colorAsArray(color) {
return {
r: parseInt(color.substring(0, 2), 16),
g: parseInt(color.substring(2, 4), 16),
b: parseInt(color.substring(4, 8), 16),
};
}
function limited(value, limit) {
return Math.ceil(value > limit ? limit : value);
}
function padRGBDigit(str) {
return ('00' + str).slice(-2);
}
});
})();
// my app
(function() {
angular.module('myApp', ['colorized'])
.controller('myController', function($scope, $colors) {
$scope.mySourceColor = '#000000';
$scope.myPercentage = 50;
$scope.myLightColor = function() {
return '#' + $colors.lighten($scope.mySourceColor, $scope.myPercentage);
};
});
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
<div ng-controller="myController">
<input type="color" ng-model="mySourceColor">
<input ng-style="{'background-color': myLightColor()}" type="range" ng-model="myPercentage" min="0" max="100">
<span>
{{ myLightColor() }}
</span>
</div>
你的顏色是什麼格式?六角/ RGB /的RGBA /命名?對於某些類型,這可能是相當簡單的數學,與其他類型可能會更復雜。 – DBS
顏色以十六進制形式出現或命名爲#FFFFFF或白色 – blue
使用十六進制值,您應該能夠將其分成它的組件('#A1B2C3' R ='A1',G ='B2',B ='C3 '),將其中的每一個轉換爲十進制'parseInt(R,16);'然後做數學運算(數字* 0.7)並且反過來進行處理以將顏色重新組合。用你命名的顏色,恐怕我不知道你怎麼能解決它。 – DBS