コード例 #1
0
        private void ContrastCal()
        {
            SolidBrush darkmoduleBrush  = qrCodeImgControl1.DarkBrush as SolidBrush;
            SolidBrush lightmoduleBrush = qrCodeImgControl1.LightBrush as SolidBrush;
            Color      darkmodule       = darkmoduleBrush == null ? _darkModule : darkmoduleBrush.Color;
            Color      lightmodule      = lightmoduleBrush == null ? _lightModule : lightmoduleBrush.Color;

            Contrast ctrast = ColorContrast.GetContrast(new FormColor(lightmodule), new FormColor(darkmodule));

            label1.Text = ctrast.Ratio.ToString();
        }
コード例 #2
0
ファイル: ColorTools.cs プロジェクト: tuukka/smuxi
        public static TextColor GetBestTextColor(TextColor fgColor,
                                                 TextColor bgColor,
                                                 ColorContrast neededContrast)
        {
            // logging noise
            //Trace.Call(fgColor, bgColor, neededContrast);

            if (fgColor == null) {
                throw new ArgumentNullException("fgColor");
            }
            if (bgColor == null) {
                throw new ArgumentNullException("bgColor");
            }

            TextColor bestColor;
            int key = fgColor.Value ^ bgColor.Value ^ (int) neededContrast;
            if (f_BestContrastColors.TryGetValue(key, out bestColor)) {
                return bestColor;
            }

            double brDiff = GetBritnessDifference(bgColor, TextColor.White);
            int modifier = 0;
            // for bright backgrounds we need to go from bright to dark colors
            // for better contrast and for dark backgrounds the opposite
            if (brDiff < 127) {
                // bright background
                modifier = -10;
            } else {
                // dark background
                modifier = 10;
            }

            double lastDifference = 0;
            bestColor = fgColor;
            int attempts = 1;
            while (true) {
                double difference = GetLuminanceDifference(bestColor, bgColor);
                double needed = ((int) neededContrast) / 10d;
                if (difference > needed) {
                    break;
                }

            #if LOG4NET
                /* logging noise
                f_Logger.Debug("GetBestTextColor(): color has bad contrast: " +
                               bestColor + " difference: " + difference +
                               " needed: " + needed);
                */
            #endif

                // change the fg color
                int red   = bestColor.Red   + modifier;
                int green = bestColor.Green + modifier;
                int blue  = bestColor.Blue  + modifier;

                // cap to allowed values
                if (modifier > 0) {
                    if (red > 255) {
                        red = 255;
                    }
                    if (green > 255) {
                        green = 255;
                    }
                    if (blue > 255) {
                        blue = 255;
                    }
                } else {
                    if (red < 0) {
                        red = 0;
                    }
                    if (green < 0) {
                        green = 0;
                    }
                    if (blue < 0) {
                        blue = 0;
                    }
                }

                bestColor = new TextColor((byte) red, (byte) green, (byte) blue);

                // in case we found no good color
                if (bestColor == TextColor.White ||
                    bestColor == TextColor.Black) {
                    break;
                }
                attempts++;
            }
            #if LOG4NET
            f_Logger.Debug(
                String.Format(
                    "GetBestTextColor(): found good contrast: {0}|{1}={2} " +
                    "({3}) attempts: {4}", fgColor, bgColor,  bestColor,
                    neededContrast, attempts
                )
            );
            #endif
            f_BestContrastColors.Add(key, bestColor);

            return bestColor;
        }