Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Replace specific color with another color in place in an image
Started by kevinlboehme Feb 03 2017 09:59 PM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

kevinlboehme

[GLOBAL: userInfoPane.html]
kevinlboehme
  • Observer

  • 19 posts
  • Corona SDK

I have an sprite that contains a specific color I would like to replace with another color in code. What is the best way of doing this in place? Should I use a custom shader? I didnt find anything in the build in effects that achieve this. Maybe I am wrong though.

 

Thanks



[TOPIC: post.html]
#2

Brent Sorrentino

[GLOBAL: userInfoPane.html]
Brent Sorrentino
  • Moderator

  • 8,164 posts
  • Corona Staff

Hi @kevinlboehme,

If a custom shader can do what you need it to (effectively make it appear that a specific color has been replaced) then yes, you should explore that route. There is no way to dynamically swap out colors of a sprite.

 

Best regards,

Brent



[TOPIC: post.html]
#3

kevinlboehme

[GLOBAL: userInfoPane.html]
kevinlboehme
  • Observer

  • 19 posts
  • Corona SDK

Thanks for the response. I figured something out that works for my purposes. This code just looks for a specific color that has a red value greater than .9 and a blue value less than .5. Its pretty awful but so was trying to get this to match on an exact RGB value! If anyone knows how to do that please advise.
 

local kernel = {}

kernel.language = "glsl"

kernel.category = "filter"

-- By default, the group is "custom"
-- kernel.group = "custom"

kernel.name = "replace"

-- Expose effect parameters using vertex data
kernel.vertexData =
{
	{
		name = "r", -- The property name exposed to Lua
		default = .5, 
		min = 0,
		max = 1,
		index = 0, -- This corresponds to CoronaVertexUserData.x
	},
	{
		name = "g", -- The property name exposed to Lua
		default = .5, 
		min = 0,
		max = 1,
		index = 1, -- This corresponds to CoronaVertexUserData.y
	},
	{
		name = "b", -- The property name exposed to Lua
		default = .5, 
		min = 0,
		max = 1,
		index = 2, -- This corresponds to CoronaVertexUserData.z
	},
}

kernel.fragment =
[[
P_COLOR vec4 FragmentKernel( P_UV vec2 texCoord ){
	P_COLOR vec4 texColor = texture2D( CoronaSampler0, texCoord );
	P_COLOR float r = CoronaVertexUserData.x;
	P_COLOR float g = CoronaVertexUserData.y;
	P_COLOR float b = CoronaVertexUserData.z;

    if (texColor[0] > 0.9 && texColor[2] < .5) {
        texColor[0] = r;
        texColor[1] = g;
	texColor[2] = b;
    }
	return CoronaColorScale(texColor);    
}
]]

return kernel



[topic_controls]
[/topic_controls]