me.BackColor = Color.FromArgb(255, 255, 255)
Dim toleranceH As Decimal = 1 Dim toleranceS As Decimal = 1 Dim toleranceV As Decimal = 1 Private Function SearchColor(ByVal couleurPipette As Color) As Color Dim indexCouleurPlusProche As Decimal = 0 Dim couleurPlusProche As Color Dim tableauCouleurs As New List(Of Color)({Color.Yellow, Color.Aqua, Color.DarkOrange, Color.DarkViolet, Color.Gray, Color.Beige, Color.BlueViolet, Color.Chartreuse, Color.Green, Color.Indigo, Color.Lime}) Dim plusPetitEcart As Integer = 255 * 3 * toleranceH * toleranceS * toleranceV + 1 Dim iCounter As Integer = 0 For Each CurrColorInArray In tableauCouleurs Dim currentCouleur As Color = CurrColorInArray Dim ecart As Integer = getEcart(couleurPipette, currentCouleur) If (ecart < plusPetitEcart) Then plusPetitEcart = ecart indexCouleurPlusProche = iCounter couleurPlusProche = currentCouleur ElseIf (ecart = 0) Then plusPetitEcart = ecart indexCouleurPlusProche = iCounter couleurPlusProche = currentCouleur Exit For End If iCounter += 1 Application.DoEvents() Next Return couleurPlusProche End Function Private Function getEcart(ByVal couleurA As Color, ByVal couleurB As Color) Dim HA As Integer = getTeinte(couleurA) Dim SA As Integer = getSaturation(couleurA) Dim VA As Integer = getLuminosite(couleurA) Dim HB As Integer = getTeinte(couleurB) Dim SB As Integer = getSaturation(couleurB) Dim VB As Integer = getLuminosite(couleurB) Dim DeltaH As Decimal = Math.Abs(HA - HB) * toleranceH Dim DeltaS As Decimal = Math.Abs(SA - SB) * toleranceS Dim DeltaV As Decimal = Math.Abs(VA - VB) * toleranceV Dim ecart As Decimal = DeltaH + DeltaS + DeltaV Return ecart End Function Private Function getTeinte(ByVal isColor As Color) As Integer Dim h As Single = isColor.GetHue() Return h End Function Private Function getSaturation(ByVal isColor As Color) As Integer Dim s As Single = isColor.GetSaturation() Return s End Function Private Function getLuminosite(ByVal isColor As Color) As Integer Dim v As Single = isColor.GetBrightness() Return v End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim TempColor As Color = Color.FromArgb(100, 20, 100, 0) PctBoxOldColor.BackColor = TempColor PctBoxNewColor.BackColor = SearchColor(TempColor) End Sub
Public Class Form1 Structure ColorName Public Color As Color Public Name As String Public Distance As Integer End Structure Public Function FindNearestKnown(ByVal c As Color) As ColorName Dim best As ColorName best.Name = Nothing For Each colorName As String In [Enum].GetNames(GetType(KnownColor)) Dim known As Color = Color.FromName(colorName) Dim dist As Integer dist = Math.Abs(CInt(c.R) - known.R) + Math.Abs(CInt(c.G) - known.G) + Math.Abs(CInt(c.B) - known.B) If best.Name Is Nothing OrElse dist < best.Distance Then best.Color = known best.Name = colorName best.Distance = dist End If Next Return best End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim R As New Random Dim randColor As Color = Color.FromArgb(R.Next(0, 255), R.Next(0, 255), R.Next(0, 255)) Me.RandomColorBox.BackColor = randColor Me.RandomColorLabel.Text = randColor.ToString '// now match to nearest known color Dim nearest As ColorName = FindNearestKnown(randColor) Me.KnownColorLabel.Text = nearest.Name Me.NearestColorBox.BackColor = nearest.Color End Sub End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questioncouleur_rapprochee_PaletteRGB = &H2000000 Or couleur_recherchee
D'un côté j'ai une palette de couleurs par défaut.
De l'autre je récupère en un point précis une couleur.
Le but recherché c'est de trouver parmi la palette de couleurs par défaut la couleur la plus ressemblante.
Le plus étonnant ? ===>> les deux couleurs ainsi obtenues (l'une par fonction de l'Api et l'autre avec l'opérateur Or), bien que différentes à la fois de la couleur recherchée et entre elles, sont en effet si proches de la couleur recherchée que la différence est imperceptible à l'oeil nu !