protected Color32 QuantizeWithOffset(Color32 c, sbyte offset) { if (Strength < 1f) { offset = (sbyte)(offset * Strength); } Color32 result = new Color32( (c.R + offset).ClipToByte(), (c.G + offset).ClipToByte(), (c.B + offset).ClipToByte()); // getting the quantized value of the dithered result // (it might be quantized further if the target image cannot represent it) return(QuantizingSession.GetQuantizedColor(result)); }
protected void CalibrateStrength(sbyte min, sbyte max) { // Calibrating strength between 0 and 1 Strength = 1f; Color32 quantizedWhite = QuantizingSession.GetQuantizedColor(Color32.White); Color32 quantizedBlack = QuantizingSession.GetQuantizedColor(Color32.Black); // Checking 1 (strongest) first. If this is alright, we are done if (CheckStrength(quantizedWhite, quantizedBlack, min, max)) { return; } // Halving the strength until we find an acceptable value while (true) { Strength /= 2f; if (CheckStrength(quantizedWhite, quantizedBlack, min, max)) { break; } } // Doing the same again with the lastly found good value as upper limit float lo = Strength; float hi = Strength * 2f; while (true) { Strength = (hi + lo) / 2f; if (CheckStrength(quantizedWhite, quantizedBlack, min, max)) { break; } hi = Strength; } }