Skip to content

Instantly share code, notes, and snippets.

View RomanEsin's full-sized avatar
:shipit:

Roman Esin RomanEsin

:shipit:
View GitHub Profile
@RomanEsin
RomanEsin / replaceColor.swift
Last active October 20, 2022 19:35 — forked from andrey-str/replaceColor.swift
Replace color in UIImage with give tolerance value
// color - source color, which is must be replaced
// withColor - target color
// tolerance - value in range from 0 to 1
func replaceColor(color: UIColor, withColor: UIColor, image: UIImage, tolerance: CGFloat) -> UIImage {
// This function expects to get source color(color which is supposed to be replaced)
// and target color in RGBA color space, hence we expect to get 4 color components: r, g, b, a
assert(color.cgColor.numberOfComponents == 4 && withColor.cgColor.numberOfComponents == 4,
"Must be RGBA colorspace")