Java Array Combine combineComponent(float[] partsComponent, float[] partsAlpha, int partsN)

Here you can find the source of combineComponent(float[] partsComponent, float[] partsAlpha, int partsN)

Description

Combine multiple values of an color component (red, green or blue).

License

Apache License

Parameter

Parameter Description
partsComponent values of the component.
partsAlpha alpha values.
partsN number of values.

Return

combined value

Declaration

public static float combineComponent(float[] partsComponent, float[] partsAlpha, int partsN) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**// www  .ja va  2 s.  c  om
     * Combine multiple values of an color component (red, green or blue).
     * 
     * @param partsComponent
     *            values of the component.
     * @param partsAlpha
     *            alpha values.
     * @param partsN
     *            number of values.
     * @return combined value
     */
    public static float combineComponent(float[] partsComponent, float[] partsAlpha, int partsN) {
        float sumComponent = 0.f;
        float sumAlpha = 0.f;
        int count = 0;

        for (int i = 0; i < partsN; i++) {
            if ((!Float.isNaN(partsComponent[i])) && (!Float.isNaN(partsAlpha[i]))) {
                sumComponent += partsComponent[i] * partsAlpha[i];
                sumAlpha += partsAlpha[i];
                count++;
            }
        }

        // fall back to the simple method without partsAlpha
        if ((count <= 0) || (sumAlpha <= 0.f)) {
            count = 0;

            for (int i = 0; i < partsN; i++) {
                if (!Float.isNaN(partsComponent[i])) {
                    sumComponent += partsComponent[i];
                    count++;
                }
            }

            sumAlpha = count;
        }

        if (count > 0) {
            return sumComponent / sumAlpha;
        } else {
            return 0.f;
        }
    }
}

Related

  1. combineArrays(final Object[] array1, final Object[] array2, final Object[] targetArray)
  2. combineArrays(float[]... arrays)
  3. combineByteArray(byte[] one, byte[] two)
  4. combineChannels(int[][] a, int[][] r, int[][] g, int[][] b, int[][] pixels)
  5. combineChunksToByteArray(byte chunks[][])
  6. combineIndices(String[] in, String delimiter, int i, int j)
  7. combineIntArrays(int[] A, int[] B)
  8. combineLines(String[] lines)