The inverse color can be calculated via exclusive OR (XOR) versus the maximum value (#FFFFFF).
The hexadecimal color format is already a hint of how to do this: each of the double octets in #RRGGBB
represents a primary color whose intensity can range from # 00 to #FF (255 in decimal).
In other words, each primary color is represented by 8 bits.
To invert a color, reverse the bits:
Cor Valor HEX valor BIN BIN invertido HEX invertido
Preto #000000 B00000000 00000000 00000000 B11111111 11111111 11111111 #FFFFFF
Branco #FFFFFF B11111111 11111111 11111111 B00000000 00000000 00000000 #000000
Azul #0000FF B00000000 00000000 11111111 B11111111 11111111 00000000 #FFFF00
An example code in Angular / javaScript that performs this operation follows:
function SampleController($scope) {
$scope.sourceValue = 0;
$scope.inverseColor = function(){
var srcVal = $scope.sourceValue;
var valNumerico = parseInt(srcVal, 16);
var mascara = parseInt('FFFFFF', 16);
var dest = valNumerico ^ mascara; //Operação XOR
return dest.toString(16);
};
}
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></head><body><divng-controller="SampleController">
Cor: #<input type='text' ng-model='sourceValue'>
<br />
Inversa: {{ inverseColor() }}
</div>
</body>
</html>