Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Lighten/Darken Brush Photoshop style  (Read 2790 times)

0 Members and 1 Guest are viewing this topic.

Wafthrudnir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Lighten/Darken Brush Photoshop style
« on: July 18, 2015, 01:04:13 am »
Hi,

I want to create a (circle) brush to lighten or darken pixels of a sprite/texture.

I thought about getting all the pixels inside the radius of my circle.
loop through the pixels from the inside to the outside and change the color depending on the distance of the circle origin.

I am struggling with a good way to implement this feature and my implementation seems to suck hard. First step is to find ALL the pixels inside the circle from a sprite ... fails :(

Here's my code:
        static List<Vector2f> FindPixelsOnSprite(CircleShape c, Sprite s)
        {
            List<Vector2f> pixels = new List<Vector2f>();

            float radius = c.Radius;
            float m1 = c.Position.X - s.Position.X;
            float m2 = c.Position.Y - s.Position.Y;

            for (float x = 0; x < s.TextureRect.Width; x++)
            {
                for (float y = 0; y < s.TextureRect.Height; y++)
                {                
                    float dx = x - m1;
                    float dy = y - m2;
                    float distance = dx * dx + dy * dy;

                    if(distance <= c.Radius)
                    {
                        pixels.Add(new Vector2f(x, y));
                    }
                }
            }

            return pixels;
        }
 

I attached my Visual Studio 2013 (Premium) project too ... set the working directory to the project directory, so the program will find the pic.png
Also add the NuGet SFML package (couldn't upload the whole solution)

Maybe you can help me out here :/

Edit:
This is the effect I want to achieve on mouse click:
« Last Edit: July 18, 2015, 01:29:45 am by Wafthrudnir »
Black Metal + Coding = Win!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Lighten/Darken Brush Photoshop style
« Reply #1 on: July 18, 2015, 01:14:36 am »
Nobody will go, download your project and just magically fix and write everything for you.
If you have a specific question then ask it. ;)

Why don't you use "actual" brushes that is some texture and blend that accordingly onto a render texture etc?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Wafthrudnir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Lighten/Darken Brush Photoshop style
« Reply #2 on: July 18, 2015, 01:22:00 am »
Hi eXpl0it3r,

I attached the project just for the case anyone is willing to help me and wants the complete code instead of the snippet I posted (makes it easier to test stuff, maybe) :)

What do you mean with "actual" brush?
If I have understood you correctly, you mean blend a circle shaped texture onto my sprite?
If I do this and resize my "brush-sprite", the areas wouldn't come out so well I guess, since my brush texture would get distorted?!

My specific question is:

How do I get all the pixels of a sprite within a circle? :D
I am doing something wrong, but can't figure it out right now, maybe someone has a good advice.


Edit:
This is the effect I want to achieve on mouse click:
« Last Edit: July 18, 2015, 01:30:54 am by Wafthrudnir »
Black Metal + Coding = Win!

Wafthrudnir

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Lighten/Darken Brush Photoshop style
« Reply #3 on: July 18, 2015, 02:21:19 am »
Got it, if anyone is interested, here's the code to find all pixels inside a circle:
        static List<Vector2f> FindPixelsOnSprite(CircleShape c, Sprite s)
        {
            List<Vector2f> results = new List<Vector2f>();
            float radius        = c.Radius;
            float width         = s.TextureRect.Width;
            float height        = s.TextureRect.Height;
            float circleCenterX = c.Position.X - s.Position.X;
            float circleCenterY = c.Position.Y - s.Position.Y;

            List<Vector2f> pixels = new List<Vector2f>((int)(width * height));

            for (float iX = 0; iX < width; iX++)
            {
                for (float iY = 0; iY < height; iY++)
                {
                    if (PixelInCircle(circleCenterX, circleCenterY, radius, iX, iY))
                        pixels.Add(new Vector2f(iX, iY));
                }
            }

            return pixels;
        }

        private static bool PixelInCircle(float circleCenterX, float circleCenterY, float radius, float pixelX, float pixelY)
        {
            float dx = circleCenterX - pixelX;
            float dy = circleCenterY - pixelY;
            dx *= dx;
            dy *= dy;
            float distanceSquared = dx + dy;
            float radiusSquared = radius * radius;
            return distanceSquared <= radiusSquared;
        }
 

After clicking, my program looks like this:


Next I have to find the distance of each pixel to the circle origin. :)
« Last Edit: July 18, 2015, 02:46:19 am by Wafthrudnir »
Black Metal + Coding = Win!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Lighten/Darken Brush Photoshop style
« Reply #4 on: July 18, 2015, 04:13:09 pm »
I believe that "actual brush" meant a sprite with the brush you want to use as a texture. Then, you draw the image and that sprite onto a render texture using a blend mode to get the effect you want. The render texture can be downloaded (captured) back to an image to be manipulated at pixel-level.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*