pixel-data-js 0.0.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/color.ts CHANGED
@@ -45,13 +45,46 @@ export function colorDistance(a: Color32, b: Color32): number {
45
45
  return dr * dr + dg * dg + db * db + da * da
46
46
  }
47
47
 
48
+ /**
49
+ * Linearly interpolates between two 32-bit colors using a floating-point weight.
50
+ * * This is the preferred method for UI animations or scenarios where high
51
+ * precision is required. It uses the standard `a + t * (b - a)` formula
52
+ * for each channel.
53
+ * @param a - The starting color as a 32-bit integer (AABBGGRR).
54
+ * @param b - The target color as a 32-bit integer (AABBGGRR).
55
+ * @param t - The interpolation factor between 0.0 and 1.0.
56
+ * @returns The interpolated 32-bit color.
57
+ */
48
58
  export function lerpColor32(a: Color32, b: Color32, t: number): Color32 {
49
59
  const r = (a & 0xFF) + t * ((b & 0xFF) - (a & 0xFF))
50
60
  const g = ((a >>> 8) & 0xFF) + t * (((b >>> 8) & 0xFF) - ((a >>> 8) & 0xFF))
51
61
  const b_ = ((a >>> 16) & 0xFF) + t * (((b >>> 16) & 0xFF) - ((a >>> 16) & 0xFF))
52
62
  const a_ = ((a >>> 24) & 0xFF) + t * (((b >>> 24) & 0xFF) - ((a >>> 24) & 0xFF))
53
63
 
54
- return packColor(r, g, b_, a_)
64
+ return ((a_ << 24) | (b_ << 16) | (g << 8) | r) >>> 0 as Color32
65
+ }
66
+
67
+ /**
68
+ * Linearly interpolates between two 32-bit colors using integer fixed-point math.
69
+ * Highly optimized for image processing and real-time blitting. It processes
70
+ * channels in parallel using bitmasks (RB and GA pairs).
71
+ * @note Subject to a 1-bit drift (rounding down) due to fast bit-shift division.
72
+ * @param src - The source (foreground) color as a 32-bit integer.
73
+ * @param dst - The destination (background) color as a 32-bit integer.
74
+ * @param w - The blend weight as a byte value from 0 to 255. Where 0 is 100% dst and 255 is 100% src
75
+ * @returns The blended 32-bit color.
76
+ */export function lerpColor32Fast(src: Color32, dst: Color32, w: number): Color32 {
77
+ const invA = 255 - w;
78
+
79
+ // Masking Red and Blue: 0x00FF00FF
80
+ // We process R and B in one go, then shift back down
81
+ const rb = (((src & 0x00FF00FF) * w + (dst & 0x00FF00FF) * invA) >>> 8) & 0x00FF00FF;
82
+
83
+ // Masking Green and Alpha: 0xFF00FF00
84
+ // We shift down first to avoid overflow, then shift back up
85
+ const ga = ((((src >>> 8) & 0x00FF00FF) * w + ((dst >>> 8) & 0x00FF00FF) * invA) >>> 8) & 0x00FF00FF;
86
+
87
+ return (rb | (ga << 8)) >>> 0 as Color32;
55
88
  }
56
89
 
57
90
  // Convert 0xAABBGGRR to #RRGGBBAA
package/src/index.ts CHANGED
@@ -1 +1,7 @@
1
+ export * from './ImageData/blend-modes'
2
+ export * from './ImageData/blit'
3
+ export * from './ImageData/mask'
4
+ export * from './ImageData/read-write-pixels'
1
5
  export * from './ImageData/serialization'
6
+ export * from './_types'
7
+ export * from './color'