• acm GBR HLV转换


    1. #include <stdio.h>
    2. #include <string>
    3. #include <iostream>
    4. #include <algorithm>
    5. #include <string.h>
    6. using namespace std;
    7. void RGB2HSV(double R, double G, double B, double &H, double &S, double &V)
    8. {
    9. // r,g,b values are from 0 to 1
    10. // h = [0,360], s = [0,1], v = [0,1]
    11. // if s == 0, then h = -1 (undefined)
    12. double min, max, delta,tmp;
    13. tmp = R>G?G:R;
    14. min = tmp>B?B:tmp;
    15. tmp = R>G?R:G;
    16. max = tmp>B?tmp:B;
    17. R/=255; G/=255; B/=255;
    18. min/=255;
    19. max /= 255;
    20. V = max; // v
    21. delta = max - min;
    22. if( max != 0 )
    23. S = delta / max; // s
    24. else
    25. {
    26. // r = g = b = 0 // s = 0, v is undefined
    27. S = 0;
    28. H = 0;
    29. return;
    30. }
    31. if (delta == 0){
    32. H = 0;
    33. return;
    34. }
    35. else if(R == max){
    36. if (G >= B)
    37. H = (G - B) / delta; // between yellow & magenta
    38. else
    39. H = (G - B) / delta + 6.0;
    40. }
    41. else if( G == max )
    42. H = 2.0 + ( B - R ) / delta; // between cyan & yellow
    43. else if (B == max)
    44. H = 4.0 + ( R - G ) / delta; // between magenta & cyan
    45. H *= 60.0; // degrees
    46. }
    47. void RGB2HSL(double R, double G, double B, double &H, double &S, double &L)
    48. {
    49. // r,g,b values are from 0 to 1
    50. // h = [0,360], s = [0,1], v = [0,1]
    51. // if s == 0, then h = -1 (undefined)
    52. double min, max, delta,tmp;
    53. tmp = R>G?G:R;
    54. min = tmp>B?B:tmp;
    55. tmp = R>G?R:G;
    56. max = tmp>B?tmp:B;
    57. R/=255; G/=255; B/=255;
    58. min/=255;
    59. max /= 255;
    60. L = (max+min)/2; // v
    61. delta = max - min;
    62. if(L==0 || delta == 0)
    63. S = 0;
    64. else if(L <= 0.5 && L >0)
    65. S =(max-min)/(2*L);
    66. else
    67. S =(max-min)/(2-2*L);
    68. if (delta == 0){
    69. H = 0;
    70. return;
    71. }
    72. else if(R == max){
    73. if (G >= B)
    74. H = (G - B) / delta; // between yellow & magenta
    75. else
    76. H = (G - B) / delta + 6.0;
    77. }
    78. else if( G == max )
    79. H = 2.0 + ( B - R ) / delta; // between cyan & yellow
    80. else if (B == max)
    81. H = 4.0 + ( R - G ) / delta; // between magenta & cyan
    82. H *= 60.0; // degrees
    83. }
    84. //函数把val转到r、g、b里面
    85. void HSV2RGB(double H, double S, double V, double &R, double &G, double &B)
    86. {
    87. //hsv to rgb
    88. S/=100;
    89. V/=100;
    90. double C = V *S;
    91. H /= 60;
    92. double tp;
    93. if(int(H) % 2 - 1 > 0) tp =int(H) % 2 - 1;
    94. else
    95. tp = 1-int(H) % 2 ;
    96. double X = C*(1- tp);
    97. double m = V-C;
    98. if(H<1)
    99. {
    100. R = (C+m+ 0.005)*255;
    101. G = (X+ m+ 0.005)*255;
    102. B = (0 +m+ 0.005)*255;
    103. return;
    104. }
    105. if(H<2)
    106. {
    107. R = (X +m+ 0.005)*255;
    108. G = (C +m+ 0.005)*255;
    109. B = (0 +m+ 0.005)*255;
    110. return;
    111. }
    112. if(H<3)
    113. {
    114. R = (0 + m+ 0.005)*255;
    115. G = (C + m+ 0.005)*255;
    116. B = (X + m+ 0.005)*255;
    117. return;
    118. }
    119. if(H<4)
    120. {
    121. R = (0 + m+ 0.005)*255;
    122. G = (X + m+ 0.005)*255;
    123. B = (C + m+ 0.005)*255;
    124. return;
    125. }
    126. if(H<5)
    127. {
    128. R = (X + m+ 0.005)*255;
    129. G = (0 + m+ 0.005)*255;
    130. B = (C + m+ 0.005)*255;
    131. return;
    132. }
    133. if(H<6)
    134. {
    135. R = (C + m + 0.005)*255;
    136. G = (0 + m + 0.005)*255;
    137. B = (X + m + 0.005)*255;
    138. return;
    139. }
    140. }
    141. void HSL2RGB(double H, double S, double L, double &R, double &G, double &B)
    142. {
    143. L/=100;
    144. S/=100;
    145. double tp;
    146. if(2*L-1 >= 0) tp = 2*L-1;
    147. else
    148. tp = 1-2*L;
    149. double C =(1-tp)*S;
    150. H /= 60;
    151. d
    152. if(int(H) % 2 - 1 > 0) tp =int(H) % 2 - 1;
    153. else
    154. tp = 1-int(H) % 2 ;
    155. double X = C*(1- tp);
    156. double m = L - C*0.5;
    157. if(H<1)
    158. {
    159. R = (C+m+ 0.005)*255;
    160. G = (X+ m+ 0.005)*255;
    161. B = (0 +m+ 0.005)*255;
    162. return;
    163. }
    164. if(H<2)
    165. {
    166. R = (X +m+ 0.005)*255;
    167. G = (C +m+ 0.005)*255;
    168. B = (0 +m+ 0.005)*255;
    169. return;
    170. }
    171. if(H<3)
    172. {
    173. R = (0 + m+ 0.005)*255;
    174. G = (C + m+ 0.005)*255;
    175. B = (X + m+ 0.005)*255;
    176. return;
    177. }
    178. if(H<4)
    179. {
    180. R = (0 + m+ 0.005)*255;
    181. G = (X + m+ 0.005)*255;
    182. B = (C + m+ 0.005)*255;
    183. return;
    184. }
    185. if(H<5)
    186. {
    187. R = (X + m+ 0.005)*255;
    188. G = (0 + m+ 0.005)*255;
    189. B = (C + m+ 0.005)*255;
    190. return;
    191. }
    192. if(H<6)
    193. {
    194. R = (C + m + 0.005)*255;
    195. G = (0 + m + 0.005)*255;
    196. B = (X + m + 0.005)*255;
    197. return;
    198. }
    199. }
    200. int main()
    201. {
    202. freopen("read.txt", "r", stdin);
    203. char str1[100], str2[100];
    204. while(cin >> str1)
    205. {
    206. double a, b, c;
    207. double tp1, tp2, tp3;
    208. //cin >> str2 >> a >> b >>c;
    209. if(strcmp(str1, "RGB") == 0)
    210. {
    211. scanf("%s", str2);
    212. if(strcmp(str2, "RGB") == 0)
    213. {
    214. scanf("%lf %lf %lf", &a, &b, &c);
    215. cout << "RGB" << ' ' <<int(a) << ' ' << int(b) << ' ' << int(c) <<endl;
    216. }
    217. else if(strcmp(str2, "HSL") == 0)
    218. {
    219. scanf("%lf %lf%% %lf%%", &a, &b, &c);
    220. double r, g, f;
    221. HSL2RGB(a, b, c, r, g, f);
    222. cout << "RGB" << ' ' << int(r+0.5) << ' ' << int(g+0.5) << ' ' << int(f+0.5) <<endl;
    223. }
    224. else
    225. {
    226. scanf("%lf %lf%% %lf%%", &a, &b, &c);
    227. double r, g, f;
    228. HSV2RGB(a, b, c, r,g, f);
    229. cout << "RGB" << ' ' << int(r+0.5) << ' ' << int(g+0.5) << ' ' << int(f+0.5) <<endl;
    230. }
    231. }
    232. else if(strcmp(str1, "HSL") == 0)
    233. {
    234. scanf("%s", str2);
    235. if(strcmp(str2, "HSL") == 0)
    236. {
    237. scanf("%lf %lf%% %lf%%", &a, &b, &c);
    238. cout << "HSL" << ' '<< int(a) << ' ' << int(b) << '%' << ' '<<int(c) << '%' <<endl;
    239. }
    240. else if(strcmp(str2, "RGB") == 0)
    241. {
    242. scanf("%lf %lf %lf", &a, &b, &c);
    243. RGB2HSL(a, b, c, tp1, tp2, tp3);
    244. cout << "HSL" << ' '<< int(tp1+0.5) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
    245. }
    246. else
    247. {
    248. scanf("%lf %lf%% %lf%%", &a, &b, &c);
    249. double r, g, f;
    250. HSV2RGB(a, b, c, r, g, f);
    251. RGB2HSL(r, g, f, tp1, tp2, tp3);
    252. cout << "HSL" << ' '<< int(a) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
    253. }
    254. }
    255. else
    256. {
    257. scanf("%s", str2);
    258. if(strcmp(str2, "RGB") == 0)
    259. {
    260. scanf("%lf %lf %lf", &a, &b, &c);
    261. RGB2HSV(a, b, c, tp1, tp2, tp3);
    262. cout << "HSV" << ' ' << int(tp1+0.5) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
    263. }
    264. else if(strcmp(str2, "HSL") == 0)
    265. {
    266. scanf("%lf %lf%% %lf%%", &a, &b, &c);
    267. double r, g, f;
    268. HSL2RGB(a, b, c, r, g, f);
    269. RGB2HSV(r, g, f, tp1, tp2, tp3);
    270. cout << "HSV" << ' '<< int(a) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
    271. }
    272. else
    273. {
    274. scanf("%lf %lf%% %lf%%", &a, &b, &c);
    275. cout << "HSV" << ' '<< int(a) << ' ' << int(b) << '%' << ' '<<int(c) << '%' <<endl;
    276. }
    277. }
    278. }
    279. return 0;
    280. }





    附件列表

    • 相关阅读:
      RollingFileAppender 日志按天记录输出到log文件
      spring boot jar包开机自启
      oracle 日常操作--触发器调试
      转载:java基础-String不可变的好处
      java 代码优化
      nginx部署ssl证书
      修改npm仓库地址
      linux下安装python3
      python一行代码开启http
      linux下安装mongodb
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/b0618c20550f930ee56b88515b825cf6.html
    Copyright © 2020-2023  润新知