| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using Unity.Burst; |
| | 6 | | using Unity.Collections; |
| | 7 | | using Unity.Jobs; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX.Jobs.ParallelFor |
| | 11 | | { |
| | 12 | | [BurstCompile] |
| | 13 | | public struct Color32CompareJob : IJobParallelFor |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// The left-hand side <see cref="Unity.Collections.NativeArray{T}" /> typed as <see cref="byte" />. |
| | 17 | | /// </summary> |
| | 18 | | [ReadOnly] public NativeArray<Color32> A; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// The right-hand side <see cref="Unity.Collections.NativeArray{T}" /> typed as <see cref="byte" />. |
| | 22 | | /// </summary> |
| | 23 | | [ReadOnly] public NativeArray<Color32> B; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The percent difference between the two values. |
| | 27 | | /// </summary> |
| | 28 | | [WriteOnly] public NativeArray<float> Percentage; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Executable work for the provided index. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="index">The index of the Parallel for loop at which to perform work.</param> |
| | 34 | | public void Execute(int index) |
| 0 | 35 | | { |
| 0 | 36 | | if (A[index].r == B[index].r && |
| | 37 | | A[index].g == B[index].g && |
| | 38 | | A[index].b == B[index].b && |
| | 39 | | A[index].a == B[index].a) |
| 0 | 40 | | { |
| 0 | 41 | | Percentage[index] = 1f; |
| 0 | 42 | | } |
| | 43 | | else |
| 0 | 44 | | { |
| 0 | 45 | | float rDiff = 1f - Mathf.Abs(A[index].r / 255 - B[index].r / 255); |
| 0 | 46 | | float gDiff = 1f - Mathf.Abs(A[index].g / 255 - B[index].g / 255); |
| 0 | 47 | | float bDiff = 1f - Mathf.Abs(A[index].b / 255 - B[index].b / 255); |
| 0 | 48 | | float aDiff = 1f - Mathf.Abs(A[index].a / 255 - B[index].a / 255); |
| | 49 | |
|
| 0 | 50 | | Percentage[index] = (rDiff + gDiff + bDiff + aDiff) / 4f; |
| 0 | 51 | | } |
| 0 | 52 | | } |
| | 53 | | } |
| | 54 | | } |