Last active
May 28, 2025 12:41
-
-
Save orange-in-space/777fd95b78095a23fe605eb187e7ebe2 to your computer and use it in GitHub Desktop.
Color Blindness Simulator(public domain by ChatGPT), ChatGPT 4oさんが書いてくれた色覚異常シミュレーションコード><
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
namespace Daltonization | |
{ | |
// Color Blindness Simulator | |
// This code was generated by ChatGPT and is in the public domain. | |
// | |
// ChatGPT 4oさんが書いてくれた色覚異常シミュレーションコード>< | |
// | |
// How to use: | |
// Bitmap protanopiaImage = MachadoSimulator.Simulate(sourceBitmap, ColorBlindnessType.Protanopia); | |
// Bitmap deuteranopiaImage = MachadoSimulator.Simulate(sourceBitmap, ColorBlindnessType.Deuteranopia); | |
// Bitmap tritanopiaImage = MachadoSimulator.Simulate(sourceBitmap, ColorBlindnessType.Tritanopia); | |
// | |
public enum ColorBlindnessType | |
{ | |
Normal, | |
Protanopia, | |
Deuteranopia, | |
Tritanopia | |
} | |
public static class MachadoSimulator | |
{ | |
private static readonly Dictionary<ColorBlindnessType, double[,]> simMatrices = new() | |
{ | |
{ | |
ColorBlindnessType.Protanopia, new double[,] | |
{ | |
{ 0.152286, 1.052583, -0.204868 }, | |
{ 0.114503, 0.786281, 0.099216 }, | |
{ -0.003882, -0.048116, 1.051998 } | |
} | |
}, | |
{ | |
ColorBlindnessType.Deuteranopia, new double[,] | |
{ | |
{ 0.367322, 0.860646, -0.227968 }, | |
{ 0.280085, 0.672501, 0.047413 }, | |
{ -0.011820, 0.042940, 0.968881 } | |
} | |
}, | |
{ | |
ColorBlindnessType.Tritanopia, new double[,] | |
{ | |
{ 1.255528, -0.076749, -0.178779 }, | |
{ -0.078411, 0.930809, 0.147602 }, | |
{ 0.004733, 0.691367, 0.303900 } | |
} | |
} | |
}; | |
public static Bitmap Simulate(Bitmap input, ColorBlindnessType type) | |
{ | |
if (type == ColorBlindnessType.Normal) return new Bitmap(input); | |
Bitmap output = new Bitmap(input.Width, input.Height); | |
double[,] matrix = simMatrices[type]; | |
for (int y = 0; y < input.Height; y++) | |
{ | |
for (int x = 0; x < input.Width; x++) | |
{ | |
Color original = input.GetPixel(x, y); | |
double r = original.R / 255.0; | |
double g = original.G / 255.0; | |
double b = original.B / 255.0; | |
double rSim = r * matrix[0, 0] + g * matrix[0, 1] + b * matrix[0, 2]; | |
double gSim = r * matrix[1, 0] + g * matrix[1, 1] + b * matrix[1, 2]; | |
double bSim = r * matrix[2, 0] + g * matrix[2, 1] + b * matrix[2, 2]; | |
int R = Clamp((int)(rSim * 255)); | |
int G = Clamp((int)(gSim * 255)); | |
int B = Clamp((int)(bSim * 255)); | |
output.SetPixel(x, y, Color.FromArgb(R, G, B)); | |
} | |
} | |
return output; | |
} | |
private static int Clamp(int val) => Math.Min(255, Math.Max(0, val)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment