color_resolver.cpp
Upload User: ziyu85
Upload Date: 2022-04-24
Package Size: 1375k
Code Size: 6k
Category:

Special Effects

Development Platform:

Visual C++

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include<math.h>
  5. #include <stdlib.h>
  6.  
  7. //#include <iomanip>
  8. char color_sort(int color_number)     //颜色分类函数
  9. {
  10. char color_class[10][10];
  11.    strcpy(color_class[0],"white");   //白色
  12.    strcpy(color_class[1],"orange");  // 橙色
  13.    strcpy(color_class[2],"brown");   // 褐色
  14.    strcpy(color_class[3],"black");   // 黑色
  15.    strcpy(color_class[4],"red");     // 红色
  16.    strcpy(color_class[5],"yellow");  //黄色
  17.    strcpy(color_class[6],"gray");    //灰色
  18.    strcpy(color_class[7],"blue");    //蓝色
  19.    strcpy(color_class[8],"green");   // 绿色
  20.    strcpy(color_class[9],"purple");  // 紫色
  21. cout<<"The color of the picture is "<<color_class[color_number]<<endl;
  22.      return(1);
  23. };
  24. double Euclid(double R,double G,double B)
  25. {
  26. double euclid;
  27. euclid=sqrt((R-G)*(R-G)+(R-B)*(R-B)+(G-B)*(G-B));//欧几里得距离
  28. return(euclid);
  29. }
  30. double color[3];
  31. double HsvToRgb(double H,double S,double V)
  32. {
  33. double var_h,var_i,var_r,var_g,var_b;
  34. double var_1,var_2,var_3;
  35. double f;
  36. if ( S == 0 )                       //HSV values = 0 ÷ 1
  37. {
  38.    color[0]= V * 255;
  39.    color[1]= V * 255;
  40.    color[2]= V * 255;
  41. }
  42. else
  43. {
  44.    var_h = H / 6;
  45.    //if ( var_h == 6 ) var_h = 0 ;     //H must be < 1
  46.    var_i = int( var_h ) ;   //Or ... var_i = floor( var_h )
  47.    f=var_h-var_i;
  48.    var_1 = V * ( 1 - S );
  49.    var_2 = V * ( 1 - S * f);
  50.    var_3 = V * ( 1 - S * ( 1 - f ) );
  51.    if      ( var_i == 0 ) { var_r = V     ; var_g = var_3 ; var_b = var_1 ;}
  52.    else if ( var_i == 1 ) { var_r = var_2 ; var_g = V     ; var_b = var_1 ;}
  53.    else if ( var_i == 2 ) { var_r = var_1 ; var_g = V     ; var_b = var_3 ;}
  54.    else if ( var_i == 3 ) { var_r = var_1 ; var_g = var_2 ; var_b = V     ;}
  55.    else if ( var_i == 4 ) { var_r = var_3 ; var_g = var_1 ; var_b = V     ;}
  56.    else                   { var_r = V     ; var_g = var_1 ; var_b = var_2 ;}
  57.    color[0]= var_r * 255;                 //RGB results = 0 ÷ 255
  58.    color[1]= var_g * 255;
  59.    color[2]= var_b * 255;
  60. }
  61. cout<<"the picture'R= "<<color[0]<<endl<<"the picture'G= "<<color[1]<<endl<<"the picture'B= "<<color[2]<<endl;//setprecision(3)<<
  62. return(color[0],color[1],color[2]);
  63. }
  64. int main()
  65. {
  66. int color_number;
  67. int R;
  68. int G;
  69. int B;
  70.     double H;
  71. double S;
  72. double V;
  73. double EUCLID;
  74.  //输入HSV值
  75. cout<<"set the value of H"<<endl;
  76. cin>>H;
  77. cout<<"set the value of S"<<endl;
  78. cin>>S;
  79. cout<<"set the value of V"<<endl;
  80. cin>>V;
  81. //调用HsvToRgb函数
  82. HsvToRgb(H,S,V);
  83. R=(int)color[0];
  84. G=(int)color[1];
  85. B=(int)color[2];
  86. //限定RGB范围
  87. if(R<0||R>255)                             
  88. {cout<<"It is wrong of R"<<endl;
  89. }
  90. if(G<0||G>255)                             
  91. {cout<<"It is wrong of G"<<endl;
  92. }
  93. if(B<0||B>255)                             
  94. {cout<<"It is wrong of B"<<endl;   
  95. }
  96. //颜色对应RGB规则
  97. if(0<=R&&R<=50)                             
  98. {if(0<=G&&G<=50)
  99.   {if(0<=B&&B<=50)
  100.        {
  101.       EUCLID=Euclid(R,G,B);
  102. if(EUCLID<=30)
  103. {
  104. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  105. color_number=3;
  106. color_sort(color_number);//黑色
  107. }}}}
  108. if(210<=R&&R<=255)                             
  109. {if(210<=G&&G<=255)
  110.   {if(170<=B&&B<=255)
  111.        {
  112.       EUCLID=Euclid(R,G,B);
  113. if(EUCLID<=80)
  114. {
  115. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  116. color_number=0;
  117. color_sort(color_number);//白色
  118. }}}}
  119. if(20<=R&&R<=210)                             
  120. {if(20<=G&&G<=210)
  121.   {if(20<=B&&B<=210)
  122.        {
  123.       EUCLID=Euclid(R,G,B);
  124. if(EUCLID<=40)
  125. {
  126. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  127. color_number=6;
  128. color_sort(color_number);//灰色
  129. }}}}
  130. if(130<=R&&R<=255)                             
  131. {if(0<=G&&G<=120)
  132.   {if(0<=B&&B<=150)
  133.        {
  134.       EUCLID=Euclid(R,G,B);
  135. if(130<=EUCLID&&EUCLID<=365)
  136. {
  137. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  138. color_number=4;
  139. color_sort(color_number);//红色
  140. }}}}
  141. if(0<=R&&R<=180)                             
  142. {if(0<=G&&G<=230)
  143.   {if(100<=B&&B<=255)
  144.        {
  145.       EUCLID=Euclid(R,G,B);
  146. if(100<=EUCLID&&EUCLID<=365)
  147. {
  148. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  149. color_number=7;
  150. color_sort(color_number);//蓝色
  151. }}}}
  152. if(0<=R&&R<=200)                             
  153. {if(100<=G&&G<=255)
  154.   {if(0<=B&&B<=200)
  155.        {
  156.       EUCLID=Euclid(R,G,B);
  157. if(60<=EUCLID&&EUCLID<=365)
  158. {
  159. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  160. color_number=8;
  161. color_sort(color_number);//绿色
  162. }}}}
  163. if(130<=R&&R<=255)                             
  164. {if(60<=G&&G<=170)
  165.   {if(0<=B&&B<=10)
  166.        {
  167.       EUCLID=Euclid(R,G,B);
  168. if(160<=EUCLID&&EUCLID<=320)
  169. {
  170. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  171. color_number=1;
  172. color_sort(color_number);//橙色
  173. }}}}
  174. if(70<=R&&R<=255)                             
  175. {if(30<=G&&G<=70)
  176.   {if(30<=B&&B<=70)
  177.        {
  178.       EUCLID=Euclid(R,G,B);
  179. if(50<=EUCLID&&EUCLID<=280)
  180. {
  181. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  182. color_number=2;
  183. color_sort(color_number);//褐色
  184. }}}}
  185. if(130<=R&&R<=255)                             
  186. {if(110<=G&&G<=255)
  187.   {if(0<=B&&B<=10)
  188.        {
  189.       EUCLID=Euclid(R,G,B);
  190. if(180<=EUCLID&&EUCLID<=360)
  191. {
  192. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  193. color_number=5;
  194. color_sort(color_number);//黄色
  195. }}}}
  196. if(90<=R&&R<=190)                             
  197. {if(0<=G&&G<=140)
  198.   {if(130<=B&&B<=250)
  199.        {
  200.       EUCLID=Euclid(R,G,B);
  201. if(80<=EUCLID&&EUCLID<=270)
  202. {
  203. cout<<"The Euclidean distance of the picture is "<<EUCLID<<endl;//欧几里得距离
  204. color_number=9;
  205. color_sort(color_number);//紫色
  206. }}}}
  207. return(1);
  208. }