2010-05-24 11 views
12

Ho fatto una ricerca di un convertitore HsbToRgb nei documenti ma non ho trovato nulla contenente "hsb" o "hsl", quindi suppongo che non esista. Per sicurezza, ci sono classi che supportano questa conversione?.NET Framework 3.5 ha un convertitore HsbToRgb o devo eseguire il rollover da solo?

Aggiornamento

ho finito per andare con questo, che è leggermente diverso da quello di 0xA3. Ho anche aggiunto uno AhsbToArgb così posso convertire in RGB e impostare il canale alfa in un colpo.

AhsbToArgb - consente di canale alfa:

public static Color AhsbToArgb(byte a, double h, double s, double b) 
{ 
    var color = HsbToRgb(h, s, b); 
    return Color.FromArgb(a, color.R, color.G, color.B); 
} 

HsbToRgb - converte tonalità-saturazione-luminosità per rosso-verde-blu:

public static Color HsbToRgb(double h, double s, double b) 
{ 
    if (s == 0) 
     return RawRgbToRgb(b, b, b); 
    else 
    { 
     var sector = h/60; 
     var sectorNumber = (int)Math.Truncate(sector); 
     var sectorFraction = sector - sectorNumber; 
     var b1 = b * (1 - s); 
     var b2 = b * (1 - s * sectorFraction); 
     var b3 = b * (1 - s * (1 - sectorFraction)); 
     switch (sectorNumber) 
     { 
      case 0: 
       return RawRgbToRgb(b, b3, b1); 
      case 1: 
       return RawRgbToRgb(b2, b, b1); 
      case 2: 
       return RawRgbToRgb(b1, b, b3); 
      case 3: 
       return RawRgbToRgb(b1, b2, b); 
      case 4: 
       return RawRgbToRgb(b3, b1, b); 
      case 5: 
       return RawRgbToRgb(b, b1, b2); 
      default: 
       throw new ArgumentException("Hue must be between 0 and 360"); 
     } 
    } 
} 

RawRgbToRgb - converte doppie a int e restituisce un colore oggetto:

private static Color RawRgbToRgb(double rawR, double rawG, double rawB) 
{ 
    return Color.FromArgb(
     (int)Math.Round(rawR * 255), 
     (int)Math.Round(rawG * 255), 
     (int)Math.Round(rawB * 255)); 
} 
+0

Facendo il pesce: http: //en.wikipedia. org/wiki/HSL_and_HSV –

risposta

14

No, non per quanto ne so. Ma l'algoritmo non è molto complicato e troverete il codice sul Web a lavorare come quella su questo articolo CodeProject su Manipulating colors in .NET da Guillaume Leparmentier:

public static Color HSBtoRGB(double hue, double saturation, double brightness) 
{ 
    double r = 0; 
    double g = 0; 
    double b = 0; 

    if (saturation == 0) 
    { 
     r = g = b = brightness; 
    } 
    else 
    { 
     // The color wheel consists of 6 sectors. 
     // Figure out which sector you're in. 
     // 
     double sectorPos = hue/60.0; 
     int sectorNumber = (int)(Math.Floor(sectorPos)); 

     // get the fractional part of the sector 
     double fractionalSector = sectorPos - sectorNumber; 

     // calculate values for the three axes of the color. 
     double p = brightness * (1.0 - saturation); 
     double q = brightness * (1.0 - (saturation * fractionalSector)); 
     double t = brightness * (1.0 - (saturation * (1 - fractionalSector))); 

     // assign the fractional colors to r, g, and b 
     // based on the sector the angle is in. 
     switch (sectorNumber) 
     { 
      case 0: 
       r = brightness; 
       g = t; 
       b = p; 
       break; 
      case 1: 
       r = q; 
       g = brightness; 
       b = p; 
       break; 
      case 2: 
       r = p; 
       g = brightness; 
       b = t; 
       break; 
      case 3: 
       r = p; 
       g = q; 
       b = brightness; 
       break; 
      case 4: 
       r = t; 
       g = p; 
       b = brightness; 
       break; 
      case 5: 
       r = brightness; 
       g = p; 
       b = q; 
       break; 
     } 
    } 

    return Color.FromArgb(
     (int)(r * 255.0 + 0.5), 
     (int)(g * 255.0 + 0.5), 
     (int)(b * 255.0 + 0.5)); 
} 
+0

Beh, questo è utile. Chiaro e al punto. – Rusty

+0

Bella risposta, grazie! – devuxer

+0

... anche se ho appena notato, questo sembra dichiarare 'b' due volte. – devuxer

5

No, in .NET (fino a 4.0 incluso) la classe Color converte automaticamente solo da RGB a HSB (tramite i metodi GetHue, GetSaturation e GetBrightness). Si deve rotolare il proprio metodo per passare da valori HSB a RGB valori, o utilizzare un esempio già scritto, come:

http://www.codeproject.com/KB/GDI-plus/HSBColorClass.aspx

Problemi correlati