Jump to content
How to Install Plugins ×

Saturation to Alpha


NinthDesertDude

Recommended Posts

Overview
This overwrites the alpha channel with saturation. Basically, it makes colors with less saturation more transparent (or vice versa). The effect can be found under the Effects --> Transparency tab.
 
EDIT: Note that my channel ops plugin can do all of this as well, so the only thing this plugin is good for is convenience if used frequently.
 
IVN4J1h.png4cytGit.png
Purpose and applications
This changes the transparency of each pixel based on the saturation to preserve colors.
This tool is very useful in making backgrounds transparent for your drawings that have color. The more saturation a pixel has, the less transparent it will appear.
 
Options
Invert Alpha: Inverts the alpha by performing (255 - alpha).
 
Remarks
Located in the Transparency folder under the Effects tab. This used to be called "color to alpha" back when it sucked. It has been highly improved by using the saturation channel instead of luminosity to convert to alpha. This is open source and derivatives are encouraged. By the way, it converts the pixels to HSV in order to get the saturation channel and if you make plugins, you might know how difficult it is (and HsvColor uses HSL, so it's not the same). If you want the source with HsvToRgb() as well, which has been excluded because I don't need it, you can get it here.
 
Source: I declare this to be public domain.



#region UICode
bool Amount1 = false; //[0,1] Invert Alpha
#endregion

//This function finds the max value of a range of floats.
private float Max(params float[] numbers)
{
    float max = Single.MinValue;

    foreach (float num in numbers)
    {
        if (num > max)
        {
            max = num;
        }
    }
    
    return max;
}

//This function finds the min value of a range of floats.
private float Min(params float[] numbers)
{
    float min = Single.MaxValue;

    foreach (float num in numbers)
    {
        if (num < min)
        {
            min = num;
        }
    }
    
    return min;
}

void RgbToHsv(float r, float g, float b, out float h, out float s, out float v)
{
    float min, max, delta;

    min = Min(r, g, ;
    max = Max(r, g, ;
    v = max;

    delta = max - min;

    if (max != 0)
    {
        s = delta / max;
    }
    else
    {
        s = 0;
        h = 0;
        return;
    }

    if (r == max)
    {
        h = (g -  / delta; // between yellow & magenta
    }
    else if (g == max)
    {
        h = 2 + (b - r) / delta; // between cyan & yellow
    }
    else
    {
        h = 4 + (r - g) / delta; // between magenta & cyan
    }
    h *= 60; // degrees
    if (h < 0)
    {
        h += 360;
    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra CurrentPixel;
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        for (int x = rect.Left; x < rect.Right; x++)
        {
            //Creates the color variables for rgb and hsv.
            float r, g, b, h, s, v;
            
            //Gets the current pixel color.
            CurrentPixel = src[x,y];
            
            //Sets the values for rgb.
            //These are 0-1 for RgbToHsv to use.
            r = (float)CurrentPixel.R / 255;
            g = (float)CurrentPixel.G / 255;
            b = (float)CurrentPixel.B / 255;
            
            //Calculates the values for hsv.
            //h is 0-360, s and v are 0-1.
            RgbToHsv(r, g, b, out h, out s, out v);
            
            //Sets the alpha values.
            if (Amount1)
            {
                CurrentPixel.A = (byte)(s * 255);
            }
            else
            {                
                CurrentPixel.A = (byte)(255 - (s * 255));
            }

            //Writes the pixels to the destination surface.
            dst[x,y] = CurrentPixel;
        }
    }
}


Saturation to Alpha.zip

Edited by AnthonyScoffler
Link to comment
Share on other sites

Recommend you choose another name.  We already have two plugins named "Color to Alpha" ;)

 

Please advise where the effect is found.  I have very full menus and can't seem to locate it.

Link to comment
Share on other sites

Recommend you choose another name.  We already have two plugins named "Color to Alpha" ;)

 

Please advise where the effect is found.  I have very full menus and can't seem to locate it.

I didn't mean to report it to you :noes:.  I thought that I hit the quote button...  Anyway, you won't get in trouble for not saying anything and all I wrote was my response in the report anyway.  Hopefully I don't get banned.  The effect is under the effects tab in the object submenu

Link to comment
Share on other sites

The accidental report is not a problem :)

Thanks for updating the post.

Ok. Found it. It's appearing under the Object menu as "Switch Color to Alpha".

1. I recommend you rename the plugin to something like  Switch Color to Alpha to differentiate from the others.

 

2. I don't really think it belongs under the Object menu.  That's for plugins which act on groups of pixels surrounded by transparency (i.e. objects).  Other options might be the Color menu (the other Color-To-Alpha's are there) or even the Transparency menu (with AlphaThreshold, Selection2clear and Tweak Transparency).  Your call.

Link to comment
Share on other sites

Thanks for your feedback and attentive responses.  It really lets me know that other people are on this site because, despite the views each topic gets, it otherwise feels rather empty.

 

1. I recommend you rename the plugin to something like  Switch Color to Alpha to differentiate from the others.

 

2. I don't really think it belongs under the Object menu.  That's for plugins which act on groups of pixels surrounded by transparency (i.e. objects).  Other options might be the Color menu (the other Color-To-Alpha's are there) or even the Transparency menu (with AlphaThreshold, Selection2clear and Tweak Transparency).  Your call.

I would rename the plugin and put it in a different category, but as I've said before, I'm trying to replicate the style of BoltBait's plugin as closely as possible.  His was in the object file, and is called "gray to alpha".  My icon is even the same as his with the small exception that it's colored.  I meant it to be used with his gray to alpha plugin or to replace it seamlessly.

Link to comment
Share on other sites

Mimicing BoltBait's plugin is not a problem. I would argue that his plugin is also probably due a relocation (he'll read this, then I'll have some explaining to do :) ).

Location aside, I think you should do a little renaming to make it clear how to find the plugin. I would change this thread title and the plugin name and the dll filename to "Switch Color to Alpha". Otherwise users are going to struggle to find it. Like I did. If the Alpha version of pdn4.0 didin't show the plugin path as a tooltip I would never have found it.

Re: forum activity. There is lots going on. Check out the Pictorium for lots of new images and posts.

Link to comment
Share on other sites

AnthonyScoffler, good to have someone else write plugins here. Not sure if yours will have any bearing on my work. The forums is humming along nicely but it would be better if people took more time to respond to posts esp. in the pictorium where people showcase their works. 

 

Galleries are a good place to see what types of works are being created & the kinds of plugins people are using & what we are lacking. I don't write plugins - no idea on codes & can barely manage a simple link. 

 

Anything that starts with Alpha makes my eyes glaze over because I just struggle with the whole concept. I'm not alone in this either. I was wondering if you could post up a couple of before & after images to show folk how to use it. Or even write a tute on it. I'm wondering how many plugins are under-utilised because people don't understand how to use them?

 

E.g. I love BoltBait's Lomography but the slider doesn't go far enough to the right (I should ask about this, shouldn't I). I don't understand what Lomography is other than I just adore the effect it gives. 

THiGVp.png

Knowledge is no burden to carry.

 

April Jones, 2012

 
Link to comment
Share on other sites

 

I would change this thread title and the plugin name and the dll filename to "Switch Color to Alpha"

How do I change the name of the thread?

Edited by AnthonyScoffler
Link to comment
Share on other sites

Plugin and  ALSO the thread.  ;)  For simplicity, keeping the thread the same name as a plugin makes locating both a great deal easier.

 

@Anthony: Edit the first post then click the button "Use Full Editor" at the foot of the editing window.  The full editor has the option to change the thread title.

Link to comment
Share on other sites

  • 8 months later...

I changed the entire plugin because the old one was junk. This one is much better and it uses the saturation channel instead of luminosity.

 

Hope nobody misses the old clutter ;).

Link to comment
Share on other sites

Thank you for the update Anthony.

 

Is it at all possible to provide an alternative way to download other than FileToLink?

I do not have (and I don't want to have) accounts with Facebook or Google+. 

 

EDIT: I downloaded your other three plugins from FileToLink without any problem.

It's just this one that asks me for verification through Fb or Google because it says it has been uploaded by an "unregistered user". Fancy that! 

Edited by Djisves

Xkds4Lh.png

Link to comment
Share on other sites

You could just upload it as a ZIP attachment to this post.

 

The Doctor: There was a goblin, or a trickster, or a warrior... A nameless, terrible thing, soaked in the blood of a billion galaxies. The most feared being in all the cosmos. And nothing could stop it, or hold it, or reason with it. One day it would just drop out of the sky and tear down your world.
Amy: But how did it end up in there?
The Doctor: You know fairy tales. A good wizard tricked it.
River Song: I hate good wizards in fairy tales; they always turn out to be him.

Link to comment
Share on other sites

 

I downloaded your other three plugins from FileToLink without any problem.

If I log in with my google account and post it, the link doesn't hassle others. I must've forgot. I updated the link.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...