All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsl-decora.Blend_OVERLAY.jsl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

// The equations are hard to get right for the overlay function and we must
// use either a Multiply or a Screen equation on each component independently
// depending on how "bright" that component is.
// As a result, rather than use conditionals for each component, we are going
// to try to fold the equations together using the equivalency that
// "Screen is an inverted Multiply of inverted components" to minimize
// the branches.
// To accomplish this we will create a vector of decision variables that
// are 0 or 1 depending on whether we want to Multiply or Screen that
// component.  We will then invert each component for which the mask is 1,
// perform a (doubled) Multiply operation, then reinvert each component for
// which the mask is 1 again.
// Inversions are of the form "invC = alpha - C" and the doubled Multiply
// equation has only the "bC * tC" component doubled (the "unaffected top"
// and "unaffected bottom" components are left alone).  See the derivation
// of the Multiply equation (A) above for more detail.
float4 blend_overlay(float4 bot, float4 top)
{
    float4 res;
    res.a = bot.a + top.a - bot.a * top.a;
    // The Screen vs. Multiply (SvM) cutoff is half of the alpha component
    // for premultiplied color components
    // mask is a 3-way test if each or any of bot colors are above SvM cutoff
    // mask.C == 0 => Multiply operation;  mask.C == 1 => Screen operation
    float3 mask = ceil(bot.rgb - bot.a * 0.5);
    // invert any colors wrt their alpha if that component is above the cutoff
    float3 adjbot = abs(bot.rgb - mask * bot.a);
    float3 adjtop = abs(top.rgb - mask * top.a);
    // Now apply the Multiply formula with the multiplication portion doubled
    res.rgb = (2.0 * adjbot + 1.0 - bot.a) * adjtop
            + (1.0 - top.a) * adjbot;
    // reinvert the result if we were originally above the Screen cutoff
    res.rgb = abs(res.rgb - mask * res.a);
    return res;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy