• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
genesys

colormath: Desaturation

15 posts in this topic

Hi! I want to desaturate my colors in a shader... the question is: What's the most performant way to do desaturation? I thought about converting from RGB to HSV and then back to RGB but the formulas i found on wikipedia look a lil bit complex - so i don't think that this is the best way to do it... Every Hint is appreciated! Thx!
0

Share this post


Link to post
Share on other sites
I'm not a maths guru but seems to me that you can do something like this:

Greyscale Intensity = 0.3 * red + 0.59 * green + 0.11 * blue

so to desaturate you would just lerp:

new_pixel.R = Intensity * K + old_pixel.R * (1 - K)
new_pixel.G = Intensity * K + old_pixel.G * (1 - K)
new_pixel.B = Intensity * K + old_pixel.B * (1 - K)

so K would range from 0.0 to 1.0 where 0 gives new pixel = old pixel and 1.0 for fully desaturated.

If I'm off the mark then sorry: I was just heading off to bed for a much needed sleep.

(Watch the debate that will follow about the constants 0.3, 0.59, 0.11)
0

Share this post


Link to post
Share on other sites
sure? how can this be?? if you multiply plain white (1.0,1.0,1.0) with these values you get (0.3,0.59,0.11) which is green!?
0

Share this post


Link to post
Share on other sites
Quote:
Original post by genesys
sure? how can this be?? if you multiply plain white (1.0,1.0,1.0) with these values you get (0.3,0.59,0.11) which is green!?
No, you do a dot product of the color and (0.3, 0.59, 0.11) to get a grayscale intensity value. If you were converting to grayscale you would set each color channel to this intensity. What the AP was suggesting is to get this grayscale intensity and then linear interpolate between it and the original color to get your desaturation.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by genesys
sure? how can this be?? if you multiply plain white (1.0,1.0,1.0) with these values you get (0.3,0.59,0.11) which is green!?


1 * .3 = .3
1 * .59 = .59
1 * .11 = .11
----------------+
intensity = .3 + .59 + .11 = 1

r = intensity
g = intensity
b = intensity
0

Share this post


Link to post
Share on other sites
if you're just trying to change the image into grayscale, I believe that taking the average of the r,g,b channels and replacing them with it, should work.

m = .33333f*(r+g+b);
r=g=b=m;
0

Share this post


Link to post
Share on other sites
Using the different weights for the red, green, and blue values (.3, .59, .11) might provide better looking results than just averaging the values, and since it is a dot product it might even be possible to calculate it as quickly as the average. The different values for each component reflect the eye's sensitivity to each color. A fully saturated green appears brighter than a fully saturated blue.
0

Share this post


Link to post
Share on other sites
In that case blend a little between the completely desaturated colour and the original colour. Do something like 0.9 * original_color + 0.1 * desaturated_color.
0

Share this post


Link to post
Share on other sites
oh ok! i understand!

taht's working :) thanks!

just one mor question: perhaps there is a less accurate but (for small saturation changes) working formula with less operations?
0

Share this post


Link to post
Share on other sites
For more colour manipulation ideas using interpolation/extrapolation, see the following: http://www.sgi.com/misc/grafica/interp/index.html

Quote:
just one mor question: perhaps there is a less accurate but (for small saturation changes) working formula with less operations?


The linear interpolations posted above can be done with a single pixel shader 1.1 instruction, you won't find much less than 1![smile]

HLSL:
const float3 coef = {0.3, 0.59, 0.11};
out.rgb = lerp(in.rgb, coef.rgb, amount);

PS1.1:
def c0, amount, 0, 0, 0
def c1, 0.3, 0.59, 0.11, 1
lrp r0.rgb, c0.r, r0.rgb, c1.rgb ; r0=input and output
0

Share this post


Link to post
Share on other sites
Quote:
The linear interpolations posted above can be done with a single pixel shader 1.1 instruction, you won't find much less than 1!
0

Share this post


Link to post
Share on other sites
Anonymous Poster: Not one instruction, you forgot to do the dot product:

(Without knowing exact HLSL syntax)

HLSL:
const float3 coef = {0.3, 0.59, 0.11};
out.rgb = lerp(in.rgb, dot3(coef.rgb, in.rgb), amount);


PS1.1:
def c0, amount, 0, 0, 0
def c1, 0.3, 0.59, 0.11, 1
; r0=input and output
dot3 r1.rgb, c1.rgb, r0.rgb
lrp r0.rgb, c0.r, r0.rgb, r1.rgb
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Anonymous Poster: Not one instruction, you forgot to do the dot product:

(Without knowing exact HLSL syntax)

HLSL:
const float3 coef = {0.3, 0.59, 0.11};
out.rgb = lerp(in.rgb, dot3(coef.rgb, in.rgb), amount);


PS1.1:
def c0, amount, 0, 0, 0
def c1, 0.3, 0.59, 0.11, 1
; r0=input and output
dot3 r1.rgb, c1.rgb, r0.rgb
lrp r0.rgb, c0.r, r0.rgb, r1.rgb


AP: Oops, my bad, yep you're quite right.


OP: So if you don't already have the luminance of the source image elsewhere in your shader, you'll have to dot the input colour against the coefficients to get it.

That said, luminance being scalar does open up some opportunities for co-issuing, so in most non-trivial pixel shaders, I'd still expect the whole desaturation operation to take only 1-1.5 effective instruction slots (or to be able to tag some extra functionality onto the operation to save elsewhere).
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0