Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active December 24, 2020 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toomasv/eee95ae869b71d221076aebf2da7bb8a to your computer and use it in GitHub Desktop.
Save toomasv/eee95ae869b71d221076aebf2da7bb8a to your computer and use it in GitHub Desktop.
Turn a color in image transparent
Red [
Description: "Make some color of image transparent"
Date: 23-December-2020
]
context [
smudge: function [b [tuple!]][
a: b - 1 c: b + 1
reduce [
charset reduce [a/1 b/1 c/1]
charset reduce [a/2 b/2 c/2]
charset reduce [a/3 b/3 c/3]
]
]
set 'transparent function [img [image!] color [tuple!]][
spec: load mold img
color: smudge color
alpha: rejoin parse spec/3/2 [
collect some [
color keep (#{FF})
| 3 skip keep (#{00})
]
]
append spec/3 alpha
do spec
]
]
@toomasv
Copy link
Author

toomasv commented Dec 23, 2020

Examples:

img1: draw 120x120 [
	anti-alias off 
	pen off 
	box 0x0 119x119 
	pen black 
	line-width 10 
	fill-pen red 
	circle 59x59 40
]

img2: draw 120x120 [
	;anti-alias off 
	line-width 10 
	fill-pen red box 19x19 49x49 
	fill-pen green box 69x19 104x49
	fill-pen yellow box 19x69 49x104 
	fill-pen orange box 69x69 104x104
]

i1: transparent img1 red

view [backdrop blue image i1]

To see what colors you have in image:

cl0: none 
clrs: sort unique parse i1/rgb [
	collect some [
		#{000000} 
	| 	#{FFFFFF} 
	| 	copy cl 3 skip 
		opt [if (cl <> cl0) (cl0: cl) keep (cl)]
	]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment