Click here to Skip to main content
15,071,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I got this project where you're recording mouse movement and later on the output is a graphic table that shows the most active and the least active sectors. Something similar to IOgraphica except I'm using rectangles and alpha color. Mouse coordinates are stored in txt file that later is being read and compared.

color.alpha is what I'm using to present the most used positions (sectors) on the monitor screen, where the minimum is 0/0.1 and maximum is 1 (0 is less used, 1 is the most used sector). There are total of 512 rectangles (32width x16height) I ran into the problem where label shows every sector with alpha 1 - printed rectangles example . The output however, is alpha being constantly 0, until some point it is 0,0,1,4,0,0,0 - output example. The output should be something like 0.2, 0.4, 0.6 and so on.

Long story short: color alpha is not changing on printed rectangles and problem is laying somewhere in this for loop with bunch of math formulas and I completely lost track of everything and I can't find the solution. I am desperate.
Project is on GitHub too.

What I have tried:

I've tried this:

C#
List<ColorManager> colorManagers = new List<ColorManager>();
            List<RectangleF> rectangles = new List<RectangleF>();
            List <Point> ListOfPoints = getAllPoints();
            int numberOfPoints = ListOfPoints.Count;
            int screenWidth = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;
            int widthStep = screenWidth / 32;
            int heightStep = screenHeight / 16;
            int currentWidthIndex = 1;
            int currentHeightIndex = 1;
            
            //print rectangles
            for (int i = 0; i < 32*16; i++)
            {
                
                RectangleF rectangle = new RectangleF(currentWidthIndex * widthStep - widthStep,
                    currentHeightIndex * heightStep -heightStep, widthStep, heightStep);
                if (currentWidthIndex == 32)
                {
                    currentHeightIndex++;
                    currentWidthIndex = 1;
                }
                else
                {
                    currentWidthIndex++;
                }

                List<Point> pointsInRectangle = ListOfPoints.FindAll(p =>
                {
                    if (p.X >= currentWidthIndex * widthStep - widthStep && p.X <= currentWidthIndex * widthStep && p.Y >= currentHeightIndex * heightStep - heightStep && p.Y <= currentHeightIndex * heightStep)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
                
                float alfa = pointsInRectangle.Count;
                System.Console.WriteLine("alfa " + alfa);
                SolidBrush sb = new SolidBrush(Color.FromArgb(pointsInRectangle.Count / numberOfPoints, 100, 0, 0));
                ColorManager cm = new ColorManager(rectangle, sb, pointsInRectangle);
                colorManagers.Add(cm);
            }
            foreach (ColorManager item in colorManagers)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(163, 8, 8)), item.rectangleF);
            }
Posted
Updated 17-Jan-21 4:12am
v3
Comments
RomanDyack 17-Jan-21 9:31am
   
Yes, What I meant was alpha transparency. The most transparent sector is the least active sector. Yet, the math doesn't work and I don't know where the problem lies.
Richard MacCutchan 17-Jan-21 9:53am
   
You need to add the alpha value to your SolidBrush when drawing the graphics objects in your ForEach loop.
RomanDyack 17-Jan-21 10:24am
   
Could you give me an example please? I tried implementing alpha value but it doesn't work. Maybe the problem is in algorithm which calculates alpha..?
Richard MacCutchan 17-Jan-21 10:36am
   
You already have an example a few lines earlier, where you create Solidbrush sb. But you need to check what values you are setting each time.
Richard MacCutchan 17-Jan-21 11:13am
   
See my updated solution.

1 solution

An alternative idea would be to use a LinearGradientBrush Class (System.Windows.Media) | Microsoft Docs[^] which gradually changes the colour.

[edit]
Here is a simple example of a gradient bar showing different levels of transparency:
C#
int interval = 16;
int barWidth = ClientRectangle.Width - 20;
barWidth /= (256 / interval);
int left = 10;
for (int i = 10; i < 256; i += 16)
{
    SolidBrush sb = new SolidBrush(Color.FromArgb(i, 192, 0, 0));
    e.Graphics.FillRectangle(sb, new Rectangle(left, 50, barWidth, 50));
    left += barWidth;
}

[/edit]
   
v2
Comments
RomanDyack 17-Jan-21 10:28am
   
Yes but what I tried was changing color transparency depending on activity of that specific sector.
Richard MacCutchan 17-Jan-21 10:37am
   
I know, but this is an alternative idea which works quite well for visual effect.
RomanDyack 18-Jan-21 9:54am
   
I just got an idea for my problem. I'll make public value for 'visit', count how many time rectangle has been visited, then set alpha = rectangle.visits/maxVisits

This is how alpha transparency value will change depending on how many times the rectangle has been "visited" by mouse. What do you think?
Richard MacCutchan 18-Jan-21 10:04am
   
Assuming your calculations are correct, then there is a simple way to find out if it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900