Advertisement
Kaelygon

Untitled

Aug 27th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <math.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. template <class T>
  10. bool isPrime(T n) //test if prime
  11. {
  12. for(T i(2); i <= sqrt(n); ++i)
  13. if (n % i == 0)
  14. return false;
  15. return true;
  16. }
  17.  
  18. int getHex(string hexstr) { //hexadecimal to decimal
  19. return (int)strtol(hexstr.c_str(), 0, 16);
  20. }
  21.  
  22. string decToHexa(int n) { //decimal to hexadecimal
  23. char hexaDeciNum[16];
  24.  
  25. int i = 0;
  26. while(n!=0){
  27. int temp = 0;
  28. temp = n % 16;
  29. if(temp < 10)
  30. {
  31. hexaDeciNum[i] = temp + 48;
  32. i++;
  33. }
  34. else
  35. {
  36. hexaDeciNum[i] = temp + 55;
  37. i++;
  38. }
  39.  
  40. n = n/16;
  41. }
  42.  
  43. string str = "";
  44. for(int j=i-1; j>=0; j--){
  45. str+=hexaDeciNum[j];
  46. }
  47. while(str.size()<2){
  48. str="0"+str;
  49. }
  50. return str;
  51. }
  52.  
  53. int main(int argc, char *argv[]){
  54.  
  55. double clos = 256*2; //The value can not be any further than 442 units. 3 dimensional distance: sqrt( 255^2*3)
  56.  
  57. for(int a=0;a<256;a++){ // a=red b=green c=blue
  58. for(int b=0;b<256;b++){
  59. for(int c=0;c<256;c++){
  60.  
  61. string str; //concate rgb hexadecimals
  62. str+=decToHexa(a);
  63. str+=decToHexa(b);
  64. str+=decToHexa(c);
  65.  
  66. int cand = getHex(str); //convert concated hex value to dec
  67. if(isPrime(cand)){ //test if it is a prime
  68.  
  69. int r2=getHex("C9"); //convert the picked color into decimals
  70. int g2=getHex("00");
  71. int b2=getHex("BD");
  72.  
  73. int r1 = a; //compare current color a,b,c with picked color
  74. int g1 = b;
  75. int b1 = c;
  76.  
  77. float d=pow( pow((r2-r1),2) + pow((g2-g1),2) + pow((b2-b1),2) , 0.5 ); //distance
  78. if(d<=clos){ //If the currently compared color is closer to the picked one
  79. clos=d; //Mark color to be closest to the picked color
  80. cout << "Closest: " << cand << " Hex: " << str << " Distance: " << d << "\n"; //print out values
  81. }
  82. }
  83.  
  84. }
  85. }
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement