image.process\color\color_adjust.mbt

100% · 233 / 233 points · 547 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 色彩调整 color adjust
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 色彩调整操作(纯 MoonBit)
  7. 7// 亮度/对比度/gamma/反色 + HSV/HSL 色彩空间转换
  8. 8
  9. 9///|
  10. 10/// 亮度调整。delta ∈ [-255, 255],正值变亮,负值变暗。
  11. 11/// 仅影响颜色通道(前 min(channels,3) 个),alpha 通道不变。
  12. 12/// 亮度调整。delta ∈ [-255, 255],正值增亮,负值减暗。 Brightness adjustment. delta ∈ [-255, 255], positive=brighter, negative=darker.
  13. 13/// 仅影响颜色通道(前 min(channels,3) 个),alpha 通道不变。 Only affects color channels (first min(channels,3)), alpha channel unchanged.
  14. 14///
  15. 15/// ```mbt check
  16. 16/// test "doc_adjust_brightness" {
  17. 17/// try {
  18. 18/// let data = Bytes::from_array([b'\x80', b'\x80', b'\x80'])
  19. 19/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
  20. 20/// let brightened = @color.adjust_brightness(img, 50)
  21. 21/// assert_eq(brightened.width, 1)
  22. 22/// assert_eq(brightened.channels, 3)
  23. 23/// } catch {
  24. 24/// _ => assert_true(false)
  25. 25/// }
  26. 26/// }
  27. 27/// ```
  28. 28pub fn adjust_brightness(
  29. 29 img : @types.Image,
  30. 30 delta : Int32,
  31. 31) -> @types.Image raise @types.LoadError {
  32. 32 // 输入验证
  33. 33● if img.width <= 0 || img.height <= 0 {
  34. 34● raise @types.LoadError::DecodeFailed(
  35. 35 "adjust_brightness: 图像尺寸必须大于0",
  36. 36 )
  37. 37 }
  38. 38 if img.channels < 1 || img.channels > 4 {
  39. 39● raise @types.LoadError::DecodeFailed(
  40. 40 "adjust_brightness: 通道数必须在1-4之间",
  41. 41 )
  42. 42 }
  43. 43 let expected_size = img.width * img.height * img.channels
  44. 44● if img.data.length() != expected_size {
  45. 45● raise @types.LoadError::DecodeFailed(
  46. 46 "adjust_brightness: 数据大小不匹配,期望 " +
  47. 47● expected_size.to_string() +
  48. 48 ",实际 " +
  49. 49● img.data.length().to_string(),
  50. 50 )
  51. 51 }
  52. 52 let n = img.width * img.height
  53. 53 let ch = img.channels
  54. 54● let color_ch = if ch >= 3 { 3 } else { ch }
  55. 55● let lut : Array[Byte] = Array::make(256, b'\x00')
  56. 56 for i in 0..<256 {
  57. 57● lut[i] = @types.clamp_byte(i + delta)
  58. 58 }
  59. 59● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  60. 60 for i in 0..<n {
  61. 61● for c in 0..<color_ch {
  62. 62● out[i * ch + c] = lut[img.data[i * ch + c].to_int()]
  63. 63 }
  64. 64 if ch == 4 {
  65. 65● out[i * ch + 3] = img.data[i * ch + 3]
  66. 66 }
  67. 67 }
  68. 68 @types.Image::{
  69. 69 width: img.width,
  70. 70 height: img.height,
  71. 71 channels: ch,
  72. 72● data: Bytes::from_array(out),
  73. 73 }
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 对比度调整。factor=1.0 为原图,>1 增加对比度,<1 降低。
  78. 78/// 以 128 为中心缩放。仅影响颜色通道,alpha 不变。
  79. 79/// 优化:直接处理所有像素,避免内层循环
  80. 80pub fn adjust_contrast(
  81. 81 img : @types.Image,
  82. 82 factor : Float,
  83. 83) -> @types.Image raise @types.LoadError {
  84. 84 // 输入验证
  85. 85● if img.width <= 0 || img.height <= 0 {
  86. 86● raise @types.LoadError::DecodeFailed(
  87. 87 "adjust_contrast: 图像尺寸必须大于0",
  88. 88 )
  89. 89 }
  90. 90 if img.channels < 1 || img.channels > 4 {
  91. 91● raise @types.LoadError::DecodeFailed(
  92. 92 "adjust_contrast: 通道数必须在1-4之间",
  93. 93 )
  94. 94 }
  95. 95 let expected_size = img.width * img.height * img.channels
  96. 96● if img.data.length() != expected_size {
  97. 97● raise @types.LoadError::DecodeFailed(
  98. 98 "adjust_contrast: 数据大小不匹配,期望 " +
  99. 99● expected_size.to_string() +
  100. 100 ",实际 " +
  101. 101● img.data.length().to_string(),
  102. 102 )
  103. 103 }
  104. 104 if factor < 0.0 {
  105. 105● raise @types.LoadError::DecodeFailed(
  106. 106 "adjust_contrast: factor必须大于等于0",
  107. 107 )
  108. 108 }
  109. 109 let n = img.width * img.height
  110. 110 let ch = img.channels
  111. 111● let _ = if ch >= 3 { 3 } else { ch }
  112. 112● let lut : Array[Byte] = Array::make(256, b'\x00')
  113. 113 for i in 0..<256 {
  114. 114● let adjusted = (Float::from_int(i - 128) * factor + 128.0F).to_int()
  115. 115● lut[i] = @types.clamp_byte(adjusted)
  116. 116 }
  117. 117 let total = n * ch
  118. 118● let out : Array[Byte] = Array::make(total, b'\x00')
  119. 119 let data = img.data
  120. 120 if ch == 4 {
  121. 121 // RGBA:处理 RGB,保留 A
  122. 122● for i in 0..<n {
  123. 123● let base = i * 4
  124. 124● out[base] = lut[data[base].to_int()]
  125. 125● out[base + 1] = lut[data[base + 1].to_int()]
  126. 126● out[base + 2] = lut[data[base + 2].to_int()]
  127. 127 out[base + 3] = data[base + 3]
  128. 128 }
  129. 129● } else if ch == 3 {
  130. 130 // RGB
  131. 131● for i in 0..<n {
  132. 132● let base = i * 3
  133. 133● out[base] = lut[data[base].to_int()]
  134. 134● out[base + 1] = lut[data[base + 1].to_int()]
  135. 135● out[base + 2] = lut[data[base + 2].to_int()]
  136. 136 }
  137. 137 } else {
  138. 138 // 单通道
  139. 139● for i in 0..<n {
  140. 140● out[i] = lut[data[i].to_int()]
  141. 141 }
  142. 142 }
  143. 143 @types.Image::{
  144. 144 width: img.width,
  145. 145 height: img.height,
  146. 146 channels: ch,
  147. 147● data: Bytes::from_array(out),
  148. 148 }
  149. 149}
  150. 150
  151. 151///|
  152. 152/// Gamma 校正。gamma=1.0 为原图,>1 变暗,<1 变亮。
  153. 153/// 使用查找表加速。仅影响颜色通道,alpha 不变。
  154. 154pub fn adjust_gamma(
  155. 155 img : @types.Image,
  156. 156 gamma : Float,
  157. 157) -> @types.Image raise @types.LoadError {
  158. 158 // 输入验证
  159. 159● if img.width <= 0 || img.height <= 0 {
  160. 160● raise @types.LoadError::DecodeFailed(
  161. 161 "adjust_gamma: 图像尺寸必须大于0",
  162. 162 )
  163. 163 }
  164. 164 if img.channels < 1 || img.channels > 4 {
  165. 165● raise @types.LoadError::DecodeFailed(
  166. 166 "adjust_gamma: 通道数必须在1-4之间",
  167. 167 )
  168. 168 }
  169. 169 let expected_size = img.width * img.height * img.channels
  170. 170● if img.data.length() != expected_size {
  171. 171● raise @types.LoadError::DecodeFailed(
  172. 172 "adjust_gamma: 数据大小不匹配,期望 " +
  173. 173● expected_size.to_string() +
  174. 174 ",实际 " +
  175. 175● img.data.length().to_string(),
  176. 176 )
  177. 177 }
  178. 178 if gamma <= 0.0 {
  179. 179● raise @types.LoadError::DecodeFailed("adjust_gamma: gamma必须大于0")
  180. 180 }
  181. 181 // 预计算 256 项查找表
  182. 182● let lut : Array[Byte] = Array::make(256, b'\x00')
  183. 183 for i in 0..<256 {
  184. 184● let normalized = Float::from_int(i) / 255.0F
  185. 185● let corrected = @math.powf(normalized, gamma)
  186. 186● let val = (corrected * 255.0F + 0.5F).to_int()
  187. 187● lut[i] = @types.clamp_byte(val)
  188. 188 }
  189. 189 let n = img.width * img.height
  190. 190 let ch = img.channels
  191. 191● let color_ch = if ch >= 3 { 3 } else { ch }
  192. 192● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  193. 193 for i in 0..<n {
  194. 194● for c in 0..<color_ch {
  195. 195● out[i * ch + c] = lut[img.data[i * ch + c].to_int()]
  196. 196 }
  197. 197 if ch == 4 {
  198. 198● out[i * ch + 3] = img.data[i * ch + 3]
  199. 199 }
  200. 200 }
  201. 201 @types.Image::{
  202. 202 width: img.width,
  203. 203 height: img.height,
  204. 204 channels: ch,
  205. 205● data: Bytes::from_array(out),
  206. 206 }
  207. 207}
  208. 208
  209. 209///|
  210. 210/// 反色(负片)。new = 255 - old。仅影响颜色通道,alpha 不变。
  211. 211pub fn invert(img : @types.Image) -> @types.Image raise @types.LoadError {
  212. 212 // 输入验证
  213. 213● if img.width <= 0 || img.height <= 0 {
  214. 214● raise @types.LoadError::DecodeFailed("invert: 图像尺寸必须大于0")
  215. 215 }
  216. 216 if img.channels < 1 || img.channels > 4 {
  217. 217● raise @types.LoadError::DecodeFailed("invert: 通道数必须在1-4之间")
  218. 218 }
  219. 219 let expected_size = img.width * img.height * img.channels
  220. 220● if img.data.length() != expected_size {
  221. 221● raise @types.LoadError::DecodeFailed(
  222. 222 "invert: 数据大小不匹配,期望 " +
  223. 223● expected_size.to_string() +
  224. 224 ",实际 " +
  225. 225● img.data.length().to_string(),
  226. 226 )
  227. 227 }
  228. 228 let n = img.width * img.height
  229. 229 let ch = img.channels
  230. 230● let color_ch = if ch >= 3 { 3 } else { ch }
  231. 231● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  232. 232 for i in 0..<n {
  233. 233● for c in 0..<color_ch {
  234. 234● out[i * ch + c] = (255 - img.data[i * ch + c].to_int()).to_byte()
  235. 235 }
  236. 236 if ch == 4 {
  237. 237● out[i * ch + 3] = img.data[i * ch + 3]
  238. 238 }
  239. 239 }
  240. 240 @types.Image::{
  241. 241 width: img.width,
  242. 242 height: img.height,
  243. 243 channels: ch,
  244. 244● data: Bytes::from_array(out),
  245. 245 }
  246. 246}
  247. 247
  248. 248///|
  249. 249/// RGB → HSV 色彩空间转换。
  250. 250/// 输入 r,g,b ∈ [0,255],输出 h ∈ [0,360), s ∈ [0,1], v ∈ [0,1]
  251. 251pub fn rgb_to_hsv(
  252. 252 r : Int32,
  253. 253 g : Int32,
  254. 254 b : Int32,
  255. 255) -> (Float, Float, Float) raise @types.LoadError {
  256. 256 // 输入验证
  257. 257● if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
  258. 258● raise @types.LoadError::DecodeFailed(
  259. 259 "rgb_to_hsv: r,g,b必须在[0,255]范围内",
  260. 260 )
  261. 261 }
  262. 262● let rf = Float::from_int(r) / 255.0F
  263. 263● let gf = Float::from_int(g) / 255.0F
  264. 264● let bf = Float::from_int(b) / 255.0F
  265. 265● let mx = rf.max(gf).max(bf)
  266. 266● let mn = rf.min(gf).min(bf)
  267. 267 let delta = mx - mn
  268. 268 let v = mx
  269. 269● let s = if mx == 0.0F { 0.0F } else { delta / mx }
  270. 270 let h = if delta == 0.0F {
  271. 271● 0.0F
  272. 272● } else if mx == rf {
  273. 273● 60.0F * ((gf - bf) / delta)
  274. 274● } else if mx == gf {
  275. 275● 60.0F * ((bf - rf) / delta + 2.0F)
  276. 276 } else {
  277. 277● 60.0F * ((rf - gf) / delta + 4.0F)
  278. 278 }
  279. 279● let h_norm = if h < 0.0F { h + 360.0F } else { h }
  280. 280 (h_norm, s, v)
  281. 281}
  282. 282
  283. 283///|
  284. 284/// HSV → RGB 色彩空间转换。
  285. 285/// 输入 h ∈ [0,360), s ∈ [0,1], v ∈ [0,1],输出 r,g,b ∈ [0,255]
  286. 286pub fn hsv_to_rgb(
  287. 287 h : Float,
  288. 288 s : Float,
  289. 289 v : Float,
  290. 290) -> (Int32, Int32, Int32) raise @types.LoadError {
  291. 291 // 输入验证
  292. 292● if s < 0.0F || s > 1.0F || v < 0.0F || v > 1.0F {
  293. 293● raise @types.LoadError::DecodeFailed(
  294. 294 "hsv_to_rgb: s,v必须在[0,1]范围内",
  295. 295 )
  296. 296 }
  297. 297 let c = v * s
  298. 298 // 归一化 h 到 [0,6)
  299. 299 let mut hp = h / 60.0F
  300. 300 while hp >= 6.0F {
  301. 301● hp = hp - 6.0F
  302. 302 }
  303. 303 while hp < 0.0F {
  304. 304● hp = hp + 6.0F
  305. 305 }
  306. 306 // hp % 2.0
  307. 307 let hp_mod2 = if hp < 2.0F {
  308. 308● hp
  309. 309● } else if hp < 4.0F {
  310. 310● hp - 2.0F
  311. 311 } else {
  312. 312● hp - 4.0F
  313. 313 }
  314. 314● let x = c * (1.0F - (hp_mod2 - 1.0F).abs())
  315. 315 let m = v - c
  316. 316 let (r1, g1, b1) = if hp < 1.0F {
  317. 317● (c, x, 0.0F)
  318. 318● } else if hp < 2.0F {
  319. 319● (x, c, 0.0F)
  320. 320● } else if hp < 3.0F {
  321. 321● (0.0F, c, x)
  322. 322● } else if hp < 4.0F {
  323. 323● (0.0F, x, c)
  324. 324● } else if hp < 5.0F {
  325. 325● (x, 0.0F, c)
  326. 326 } else {
  327. 327● (c, 0.0F, x)
  328. 328 }
  329. 329 (
  330. 330● ((r1 + m) * 255.0F + 0.5F).to_int(),
  331. 331● ((g1 + m) * 255.0F + 0.5F).to_int(),
  332. 332● ((b1 + m) * 255.0F + 0.5F).to_int(),
  333. 333 )
  334. 334}
  335. 335
  336. 336///|
  337. 337/// RGB → HSL 色彩空间转换。
  338. 338/// 输入 r,g,b ∈ [0,255],输出 h ∈ [0,360), s ∈ [0,1], l ∈ [0,1]
  339. 339pub fn rgb_to_hsl(
  340. 340 r : Int32,
  341. 341 g : Int32,
  342. 342 b : Int32,
  343. 343) -> (Float, Float, Float) raise @types.LoadError {
  344. 344 // 输入验证
  345. 345● if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
  346. 346● raise @types.LoadError::DecodeFailed(
  347. 347 "rgb_to_hsl: r,g,b必须在[0,255]范围内",
  348. 348 )
  349. 349 }
  350. 350● let rf = Float::from_int(r) / 255.0F
  351. 351● let gf = Float::from_int(g) / 255.0F
  352. 352● let bf = Float::from_int(b) / 255.0F
  353. 353● let mx = rf.max(gf).max(bf)
  354. 354● let mn = rf.min(gf).min(bf)
  355. 355 let delta = mx - mn
  356. 356 let l = (mx + mn) / 2.0F
  357. 357 let s = if delta == 0.0F {
  358. 358● 0.0F
  359. 359 } else {
  360. 360● delta / (1.0F - (2.0F * l - 1.0F).abs())
  361. 361 }
  362. 362 let h = if delta == 0.0F {
  363. 363● 0.0F
  364. 364● } else if mx == rf {
  365. 365● 60.0F * ((gf - bf) / delta)
  366. 366● } else if mx == gf {
  367. 367● 60.0F * ((bf - rf) / delta + 2.0F)
  368. 368 } else {
  369. 369● 60.0F * ((rf - gf) / delta + 4.0F)
  370. 370 }
  371. 371● let h_norm = if h < 0.0F { h + 360.0F } else { h }
  372. 372 (h_norm, s, l)
  373. 373}
  374. 374
  375. 375///|
  376. 376/// HSL → RGB 色彩空间转换。
  377. 377/// 输入 h ∈ [0,360), s ∈ [0,1], l ∈ [0,1],输出 r,g,b ∈ [0,255]
  378. 378pub fn hsl_to_rgb(
  379. 379 h : Float,
  380. 380 s : Float,
  381. 381 l : Float,
  382. 382) -> (Int32, Int32, Int32) raise @types.LoadError {
  383. 383 // 输入验证
  384. 384● if s < 0.0F || s > 1.0F || l < 0.0F || l > 1.0F {
  385. 385● raise @types.LoadError::DecodeFailed(
  386. 386 "hsl_to_rgb: s,l必须在[0,1]范围内",
  387. 387 )
  388. 388 }
  389. 389● let c = (1.0F - (2.0F * l - 1.0F).abs()) * s
  390. 390 // 归一化 h 到 [0,6)
  391. 391 let mut hp = h / 60.0F
  392. 392 while hp >= 6.0F {
  393. 393● hp = hp - 6.0F
  394. 394 }
  395. 395 while hp < 0.0F {
  396. 396● hp = hp + 6.0F
  397. 397 }
  398. 398 let hp_mod2 = if hp < 2.0F {
  399. 399● hp
  400. 400● } else if hp < 4.0F {
  401. 401● hp - 2.0F
  402. 402 } else {
  403. 403● hp - 4.0F
  404. 404 }
  405. 405● let x = c * (1.0F - (hp_mod2 - 1.0F).abs())
  406. 406 let m = l - c / 2.0F
  407. 407 let (r1, g1, b1) = if hp < 1.0F {
  408. 408● (c, x, 0.0F)
  409. 409● } else if hp < 2.0F {
  410. 410● (x, c, 0.0F)
  411. 411● } else if hp < 3.0F {
  412. 412● (0.0F, c, x)
  413. 413● } else if hp < 4.0F {
  414. 414● (0.0F, x, c)
  415. 415● } else if hp < 5.0F {
  416. 416● (x, 0.0F, c)
  417. 417 } else {
  418. 418● (c, 0.0F, x)
  419. 419 }
  420. 420 (
  421. 421● ((r1 + m) * 255.0F + 0.5F).to_int(),
  422. 422● ((g1 + m) * 255.0F + 0.5F).to_int(),
  423. 423● ((b1 + m) * 255.0F + 0.5F).to_int(),
  424. 424 )
  425. 425}
  426. 426
  427. 427///|
  428. 428/// 饱和度调整。factor: 0.0 = 灰度, 1.0 = 原始, >1.0 = 增强饱和度
  429. 429pub fn adjust_saturation(
  430. 430 img : @types.Image,
  431. 431 factor : Float,
  432. 432) -> @types.Image raise @types.LoadError {
  433. 433 // 输入验证
  434. 434● if img.width <= 0 || img.height <= 0 {
  435. 435● raise @types.LoadError::DecodeFailed(
  436. 436 "adjust_saturation: 图像尺寸必须大于0",
  437. 437 )
  438. 438 }
  439. 439 if img.channels < 1 || img.channels > 4 {
  440. 440● raise @types.LoadError::DecodeFailed(
  441. 441 "adjust_saturation: 通道数必须在1-4之间",
  442. 442 )
  443. 443 }
  444. 444 let expected_size = img.width * img.height * img.channels
  445. 445● if img.data.length() != expected_size {
  446. 446● raise @types.LoadError::DecodeFailed(
  447. 447 "adjust_saturation: 数据大小不匹配,期望 " +
  448. 448● expected_size.to_string() +
  449. 449 ",实际 " +
  450. 450● img.data.length().to_string(),
  451. 451 )
  452. 452 }
  453. 453 let n = img.width * img.height
  454. 454 let ch = img.channels
  455. 455 if ch < 3 {
  456. 456● return img
  457. 457 }
  458. 458● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  459. 459● let f = factor.clamp(min=0.0F, max=10.0F)
  460. 460 for i in 0..<n {
  461. 461● let r = Float::from_int(img.data[i * ch].to_int()) / 255.0F
  462. 462● let g = Float::from_int(img.data[i * ch + 1].to_int()) / 255.0F
  463. 463● let b = Float::from_int(img.data[i * ch + 2].to_int()) / 255.0F
  464. 464 let gray = 0.299F * r + 0.587F * g + 0.114F * b
  465. 465● let nr = (gray + (r - gray) * f).clamp(min=0.0F, max=1.0F)
  466. 466● let ng = (gray + (g - gray) * f).clamp(min=0.0F, max=1.0F)
  467. 467● let nb = (gray + (b - gray) * f).clamp(min=0.0F, max=1.0F)
  468. 468● out[i * ch] = (nr * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  469. 469 out[i * ch + 1] = (ng * 255.0F + 0.5F)
  470. 470● .to_int()
  471. 471● .clamp(min=0, max=255)
  472. 472● .to_byte()
  473. 473 out[i * ch + 2] = (nb * 255.0F + 0.5F)
  474. 474● .to_int()
  475. 475● .clamp(min=0, max=255)
  476. 476● .to_byte()
  477. 477 if ch == 4 {
  478. 478● out[i * ch + 3] = img.data[i * ch + 3]
  479. 479 }
  480. 480 }
  481. 481 @types.Image::{
  482. 482 width: img.width,
  483. 483 height: img.height,
  484. 484 channels: ch,
  485. 485● data: Bytes::from_array(out),
  486. 486 }
  487. 487}
  488. 488
  489. 489///|
  490. 490/// 色相调整。delta: 色相偏移量(度,0-360)
  491. 491pub fn adjust_hue(
  492. 492 img : @types.Image,
  493. 493 delta : Float,
  494. 494) -> @types.Image raise @types.LoadError {
  495. 495 // 输入验证
  496. 496● if img.width <= 0 || img.height <= 0 {
  497. 497● raise @types.LoadError::DecodeFailed(
  498. 498 "adjust_hue: 图像尺寸必须大于0",
  499. 499 )
  500. 500 }
  501. 501 if img.channels < 1 || img.channels > 4 {
  502. 502● raise @types.LoadError::DecodeFailed(
  503. 503 "adjust_hue: 通道数必须在1-4之间",
  504. 504 )
  505. 505 }
  506. 506 let expected_size = img.width * img.height * img.channels
  507. 507● if img.data.length() != expected_size {
  508. 508● raise @types.LoadError::DecodeFailed(
  509. 509 "adjust_hue: 数据大小不匹配,期望 " +
  510. 510● expected_size.to_string() +
  511. 511 ",实际 " +
  512. 512● img.data.length().to_string(),
  513. 513 )
  514. 514 }
  515. 515 let n = img.width * img.height
  516. 516 let ch = img.channels
  517. 517 if ch < 3 {
  518. 518● return img
  519. 519 }
  520. 520● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  521. 521 for i in 0..<n {
  522. 522● let r = img.data[i * ch].to_int()
  523. 523● let g = img.data[i * ch + 1].to_int()
  524. 524● let b = img.data[i * ch + 2].to_int()
  525. 525● let (h, s, l) = rgb_to_hsl(r, g, b)
  526. 526 let mut nh = h + delta
  527. 527 while nh >= 360.0F {
  528. 528● nh = nh - 360.0F
  529. 529 }
  530. 530 while nh < 0.0F {
  531. 531● nh = nh + 360.0F
  532. 532 }
  533. 533● let (nr, ng, nb) = hsl_to_rgb(nh, s, l)
  534. 534● out[i * ch] = nr.clamp(min=0, max=255).to_byte()
  535. 535● out[i * ch + 1] = ng.clamp(min=0, max=255).to_byte()
  536. 536● out[i * ch + 2] = nb.clamp(min=0, max=255).to_byte()
  537. 537 if ch == 4 {
  538. 538● out[i * ch + 3] = img.data[i * ch + 3]
  539. 539 }
  540. 540 }
  541. 541 @types.Image::{
  542. 542 width: img.width,
  543. 543 height: img.height,
  544. 544 channels: ch,
  545. 545● data: Bytes::from_array(out),
  546. 546 }
  547. 547}