Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- template <class T>
- bool isPrime(T n) //test if prime
- {
- for(T i(2); i <= sqrt(n); ++i)
- if (n % i == 0)
- return false;
- return true;
- }
- int getHex(string hexstr) { //hexadecimal to decimal
- return (int)strtol(hexstr.c_str(), 0, 16);
- }
- string decToHexa(int n) { //decimal to hexadecimal
- char hexaDeciNum[16];
- int i = 0;
- while(n!=0){
- int temp = 0;
- temp = n % 16;
- if(temp < 10)
- {
- hexaDeciNum[i] = temp + 48;
- i++;
- }
- else
- {
- hexaDeciNum[i] = temp + 55;
- i++;
- }
- n = n/16;
- }
- string str = "";
- for(int j=i-1; j>=0; j--){
- str+=hexaDeciNum[j];
- }
- while(str.size()<2){
- str="0"+str;
- }
- return str;
- }
- int main(int argc, char *argv[]){
- double clos = 256*2; //The value can not be any further than 442 units. 3 dimensional distance: sqrt( 255^2*3)
- for(int a=0;a<256;a++){ // a=red b=green c=blue
- for(int b=0;b<256;b++){
- for(int c=0;c<256;c++){
- string str; //concate rgb hexadecimals
- str+=decToHexa(a);
- str+=decToHexa(b);
- str+=decToHexa(c);
- int cand = getHex(str); //convert concated hex value to dec
- if(isPrime(cand)){ //test if it is a prime
- int r2=getHex("C9"); //convert the picked color into decimals
- int g2=getHex("00");
- int b2=getHex("BD");
- int r1 = a; //compare current color a,b,c with picked color
- int g1 = b;
- int b1 = c;
- float d=pow( pow((r2-r1),2) + pow((g2-g1),2) + pow((b2-b1),2) , 0.5 ); //distance
- if(d<=clos){ //If the currently compared color is closer to the picked one
- clos=d; //Mark color to be closest to the picked color
- cout << "Closest: " << cand << " Hex: " << str << " Distance: " << d << "\n"; //print out values
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement