Skip to content

Instantly share code, notes, and snippets.

@behreajj
behreajj / perceptualTransform2.cs
Created November 10, 2021 20:58
Perceptual Transforms, Cont.
static Vector4 LinearToXyz (in Color c)
{
// This could also be written as matrix-vector multiplication.
// Perceptual luminance is the y component.
return new Vector4 (
0.41241086f * c.r + 0.35758457f * c.g + 0.1804538f * c.b,
0.21264935f * c.r + 0.71516913f * c.g + 0.07218152f * c.b,
0.019331759f * c.r + 0.11919486f * c.g + 0.95039004f * c.b,
c.a);
}
@behreajj
behreajj / lutGen.cs
Created November 10, 2021 04:21
Linear Gamma Luts
byte[ ] stlLut = new byte[256];
byte[ ] ltsLut = new byte[256];
for (int i = 0; i < 256; ++i)
{
float x = i / 255.0f;
float y = StandardToLinearChannel (x);
stlLut[i] = (byte) (0.5f + 255.0f * y);
float z = LinearToStandardChannel (x);
ltsLut[i] = (byte) (0.5f + 255.0f * z);
@behreajj
behreajj / Grayscale.cs
Last active November 10, 2021 03:41
Unity Grayscale Setup
using UnityEngine;
[RequireComponent (typeof (SpriteRenderer))]
public class Grayscale : MonoBehaviour
{
void Start ( )
{
SpriteRenderer renderer = GetComponent<SpriteRenderer> ( );
Sprite sprite = renderer.sprite;
Texture2D srcTexture = sprite.texture;
@behreajj
behreajj / lumWithConvert.cs
Created November 10, 2021 03:22
Perceptual Luminance With Conversion
static float StandardToLinearChannel (in float x)
{
return x > 0.04045f ?
Mathf.Pow ((x + 0.055f) / 1.055f, 2.4f) :
x / 12.92f;
}
static float LinearToStandardChannel (in float x)
{
return x > 0.0031308f ?
@behreajj
behreajj / lumIncorrect.cs
Last active November 10, 2021 03:15
Perceptual Luminance Incorrect
static float GrayLumIncorrect (in Color srgb)
{
return 0.21264935f * srgb.r + 0.71516913f * srgb.g + 0.07218152f * srgb.b;
}
@behreajj
behreajj / hslGray.cs
Last active November 10, 2021 03:16
HSL Gray
static float GrayHsl (in Color c)
{
return 0.5f * (Mathf.Max (c.r, c.g, c.b) +
Mathf.Min (c.r, c.g, c.b));
}
@behreajj
behreajj / hsvGray.cs
Last active November 10, 2021 03:16
HSV Gray
static float GrayHsv (in Color c)
{
return Mathf.Max (c.r, c.g, c.b);
}
@behreajj
behreajj / averageGray.cs
Last active November 10, 2021 03:16
Naive Method: Average Gray
static float GrayAverage (in Color c)
{
return (c.r + c.g + c.b) / 3.0f;
}
@behreajj
behreajj / convertAseDlg.lua
Created September 9, 2021 12:41
Ase Dialog Convert
dofile("./sRgbToAdobeRgb.lua")
local dlg = Dialog { title = "SRGB - Adobe Convert" }
dlg:button {
id = "palAdobeToSrgb",
text = "Pal &Adobe to sRGB",
onclick = function()
local sprite = app.activeSprite
if sprite then
@behreajj
behreajj / colorConvert.lua
Created September 9, 2021 12:40
Convert sRGB AdobeRGB
-- Ported from the Python code of Mark Ransom
-- https://stackoverflow.com/a/40231268
-- For more on the magic numbers, see
-- http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
ColorConvert = {}
ColorConvert.__index = ColorConvert
setmetatable(ColorConvert, {
__call = function (cls, ...)