Make foregroundcolor black or white depending on background

后端 未结 5 2050
不知归路
不知归路 2020-11-30 22:23

Something like calculating the average value of rgb components and then decide whether to use black or white?

Do I have to convert RGB to HSV in first step \'cause R

5条回答
  •  不知归路
    2020-11-30 22:59

    If I'm understanding correctly, one approach might be to get hold of the desktop wallpaper image, check in some manor what colour it is and then change your application colour based on that.

    There's an article on geekpedia about getting hold of the current desktop wallpaper (and lots of hits on google on that), but the basic premise is to grab the registry value of the current wallpaper:

    RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);
    string WallpaperPath = rkWallPaper.GetValue("WallPaper").ToString();
    

    You could then use that path to open the image. You can then get hold of lots of properties, such as the dimensions and individual pixel colours in RGB.

    To work out whether it's "more white" or "more black" you have many options.

    One idea would be to get each pixel colour in RGB, average the values (to get the greyscale value) and then average the greyscale value of each pixel across the image. If this comes out > 128 then it could be condidered to be "white". If < 128 then "black". Basically you are deciding which side of the mid-grey dividing line the images pixel intensities average out to.

    // Psudo code - can't check the C# spec atm.
    foreach(Pixel p in image)
    {
        // get average of colour components.
        double greyScale = (p.Red + p.Green + p.Blue) / 3;
        totalIntensity += greyScale;
    }
    
    double averageImageIntensity = totalIntensity / image.NumPixels;
    
    if(totalIntensity > 128) // image could be considered to be "white"
    else // image could be considered "black".
    

    Problems: could be a slow procedure, you might want to sample only some of the pixels (say, every 10th one etc.) to save time. More generally, it seems like a fairly hacky thing to be doing at all. Pulling user files at run-time and messing with them is hardly clean, and it provides potential security and stability concerns. What if the image is duff or corrupt etc.

    Personally I'd suggest simply giving the user the choice of colour / theme / skin themselves, or better yet let them customise it!

提交回复
热议问题