SORU
21 Kasım 2008, Cuma


Grafikleri oldukça farklı RGB renkleri oluşturmak

Grafikler üreten ve farklı veri setleri genellikle rengine göre ayarlar fark etmek iyi bir fikir gösterirken. Bir kırmızı çizgi ve yanında yeşil ve benzeri. Sorun veri sayısı bilinmeyen bir rastgele bu renkler oluşturmak için gereken ve genellikle birbirlerine çok yakın bulurlar o zaman (yeşil, örneğin açık yeşil).

Bu çözülebilir ve belirgin farklı renkler oluşturmak için possibler olur bir fikrin var mı?

Eğer herhangi bir örnek (eğer daha kolay bulmak sadece sorunu tartışmak ve örnekler ile çözüm için çekinmeyin) C olsaydı iyi olurdu# ve temel renk RGB.

CEVAP
21 Kasım 2008, Cuma


Üç renk kanalları 0 255 R, G ve B sahip

İlk geçmesi

0, 0, 255
0, 255, 0
255, 0, 0

Sonra geçiyor

0, 255, 255
255, 0, 255
255, 255, 0

Bölmek 2 = ^ o zaman . 128 ve baştan başlayın:

0, 0, 128
0, 128, 0
128, 0, 0
0, 128, 128
128, 0, 128
128, 128, 0

2 = ^ böl . 64

Bir dahaki sefere 128 = ^ 64 ekleyin . 192

desen izleyin.

Basit bir program ve oldukça farklı renkler verir.

EDİT: kod örneği için Talep

Eğer gri bir renk kabul edilebilir ise - ek desen aşağıdaki gibi ekleme de:

255, 255, 255
128, 128, 128 

Bu kod oluşturma işleyebilir bir kaç yolu vardır.

Kolay Yolu

Eğer hiç renk sabit bir sayı daha fazla ihtiyacın olacağını garanti edemez, sadece bu desen aşağıdaki renkler bir dizi oluşturmak ve bu kullanın:

    static string[] ColourValues = new string[] { 
        "FF0000", "00FF00", "0000FF", "FFFF00", "FF00FF", "00FFFF", "000000", 
        "800000", "008000", "000080", "808000", "800080", "008080", "808080", 
        "C00000", "00C000", "0000C0", "C0C000", "C000C0", "00C0C0", "C0C0C0", 
        "400000", "004000", "000040", "404000", "400040", "004040", "404040", 
        "200000", "002000", "000020", "202000", "200020", "002020", "202020", 
        "600000", "006000", "000060", "606000", "600060", "006060", "606060", 
        "A00000", "00A000", "0000A0", "A0A000", "A000A0", "00A0A0", "A0A0A0", 
        "E00000", "00E000", "0000E0", "E0E000", "E000E0", "00E0E0", "E0E0E0", 
    };

Zor Yoldan

Ne çok ihtiyacımız olacak biliyor musun eğer doğru değilse, aşağıdaki kodu 896 renk bu desen kullanarak oluşturur. (896 = 256 * 7 / 2) 256 kanal başına renk alanı, 7 desenleri ve renkleri sadece 1 renk değeri ile ayrılmış geçmeden önce durduracağız.

Muhtemelen ihtiyacım vardı bu kodu daha çalışması yaptım. İlk, 255, desen daha sonra yukarıda açıklandığı gibi başına değerleri oluşturur başlayan yoğunluk bir jeneratör var. Desen jeneratör sadece yedi renkli desenler döngüsü.

using System;

class Program {
    static void Main(string[] args) {
        ColourGenerator generator = new ColourGenerator();
        for (int i = 0; i < 896; i  ) {
            Console.WriteLine(string.Format("{0}: {1}", i, generator.NextColour()));
        }
    }
}

public class ColourGenerator {

    private int index = 0;
    private IntensityGenerator intensityGenerator = new IntensityGenerator();

    public string NextColour() {
        string colour = string.Format(PatternGenerator.NextPattern(index),
            intensityGenerator.NextIntensity(index));
        index  ;
        return colour;
    }
}

public class PatternGenerator {
    public static string NextPattern(int index) {
        switch (index % 7) {
        case 0: return "{0}0000";
        case 1: return "00{0}00";
        case 2: return "0000{0}";
        case 3: return "{0}{0}00";
        case 4: return "{0}00{0}";
        case 5: return "00{0}{0}";
        case 6: return "{0}{0}{0}";
        default: throw new Exception("Math error");
        }
    }
}

public class IntensityGenerator {
    private IntensityValueWalker walker;
    private int current;

    public string NextIntensity(int index) {
        if (index == 0) {
            current = 255;
        }
        else if (index % 7 == 0) {
            if (walker == null) {
                walker = new IntensityValueWalker();
            }
            else {
                walker.MoveNext();
            }
            current = walker.Current.Value;
        }
        string currentText = current.ToString("X");
        if (currentText.Length == 1) currentText = "0"   currentText;
        return currentText;
    }
}

public class IntensityValue {

    private IntensityValue mChildA;
    private IntensityValue mChildB;

    public IntensityValue(IntensityValue parent, int value, int level) {
        if (level > 7) throw new Exception("There are no more colours left");
        Value = value;
        Parent = parent;
        Level = level;
    }

    public int Level { get; set; }
    public int Value { get; set; }
    public IntensityValue Parent { get; set; }

    public IntensityValue ChildA {
        get {
            return mChildA ?? (mChildA = new IntensityValue(this, this.Value - (1<<(7-Level)), Level 1));
        }
    }

    public IntensityValue ChildB {
        get {
            return mChildB ?? (mChildB = new IntensityValue(this, Value   (1<<(7-Level)), Level 1));
        }
    }
}

public class IntensityValueWalker {

    public IntensityValueWalker() {
        Current = new IntensityValue(null, 1<<7, 1);
    }

    public IntensityValue Current { get; set; }

    public void MoveNext() {
        if (Current.Parent == null) {
            Current = Current.ChildA;
        }
        else if (Current.Parent.ChildA == Current) {
            Current = Current.Parent.ChildB;
        }
        else {
            int levelsUp = 1;
            Current = Current.Parent;
            while (Current.Parent != null && Current == Current.Parent.ChildB) {
                Current = Current.Parent;
                levelsUp  ;
            }
            if (Current.Parent != null) {
                Current = Current.Parent.ChildB;
            }
            else {
                levelsUp  ;
            }
            for (int i = 0; i < levelsUp; i  ) {
                Current = Current.ChildA;
            }

        }
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • DrakeVEVO

    DrakeVEVO

    17 AĞUSTOS 2009
  • Elefant Traks

    Elefant Trak

    5 HAZİRAN 2007
  • olinerd

    olinerd

    23 AĞUSTOS 2007