Skip to content

Instantly share code, notes, and snippets.

@Spirit-act
Last active June 4, 2023 07:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spirit-act/c9bc6e89b6eb587846086824801eadbc to your computer and use it in GitHub Desktop.
Save Spirit-act/c9bc6e89b6eb587846086824801eadbc to your computer and use it in GitHub Desktop.
complementary color calculator
using System;
public class Program
{
public static void Main()
{
int[] rgb_color = new int[]{255, 0, 255};
string hex_color = "#FF00FF";
Console.WriteLine(calculate(hex_color));
int[] x = calculate(rgb_color);
Console.WriteLine("" + x[0]);
Console.WriteLine("" + x[1]);
Console.WriteLine("" + x[2]);
}
public static int[] calculate(int[] color) {
return new int[] {255 - color[0], 255 - color[1], 255 - color[2]};
}
public static string calculate(string color) {
return "#" +
(255 - Convert.ToInt32(color.Substring(1, 2), 16)).ToString("X2") +
(255 - Convert.ToInt32(color.Substring(3, 2), 16)).ToString("X2") +
(255 - Convert.ToInt32(color.Substring(5, 2), 16)).ToString("X2");
}
}
var rgb_color = [255, 0, 255];
var hex_color = '#FF00FF';
function calculate(color, hex = false) {
if (hex) {
return '#' +
("0" + (255 - parseInt(color.substring(1, 3), 16)).toString(16)).slice(-2) +
("0" + (255 - parseInt(color.substring(3, 5), 16)).toString(16)).slice(-2) +
("0" + (255 - parseInt(color.substring(5, 7), 16)).toString(16)).slice(-2);
} else {
return [(255 - color[0]), (255 - color[1]), (255 - color[2])];
}
}
console.log(calculate(rgb_color));
console.log(calculate(hex_color,true));
<?php
$rgb_color = [255, 0, 255];
$hex_color = '#FF00FF';
function calculate($color, bool $hex = false) {
if ($hex) {
return '#' .
str_pad(dechex(255 - hexdec(substr($color, 1, 2))), 2, '0', STR_PAD_LEFT) .
str_pad(dechex(255 - hexdec(substr($color, 3, 2))), 2, '0', STR_PAD_LEFT) .
str_pad(dechex(255 - hexdec(substr($color, 5, 2))), 2, '0', STR_PAD_LEFT);
} else {
return [(255 - $color[0]), (255 - $color[1]), (255 - $color[2])];
}
}
print_r(calculate($rgb_color));
print_r(calculate($hex_color, true));
rgb_color: list = [255, 0, 255]
hex_color: str = '#FF00FF'
def calculate(color, hex: bool = False, **kwargs):
if hex:
return '#' +
format(255 - int(color[1:3], 16), '02x') +
format(255 - int(color[3:5], 16), '02x') +
format(255 - int(color[5:7], 16), '02x')
else:
return [255 - x for x in color]
print(calculate(rgb_color))
print(calculate(hex_color, True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment