Tabela de cores completa

0 respostas
N

Olá pessoal.

Quem nunca analisou e viu que existem 16777215 possíveis cores nas cores formadas pela combinação rgb?
Agora, quem nunca quis ter todas essas cores para escolher qual a melhor a ser usada?

Poisé, existem por ai algumas tabelas de cores muito boas, contudo não são completas dadas as proporções de quantidades de cores.
Contudo pensando nisso resolvi criar um código que gerasse as cores (isso ainda quando trabalhava com php), na época tive muitos problemas com timeout do php e navegadores entrando em crash devido a quantidade de dados.

Esses dias resolvi criar isso em Java, pensando eu ser mais robusto e aguentar o tranco :P.

Errado, deu muitos problemas de OutOfMemory, mais mesmo assim consegui completar o código e formar um arquivo html de 624MB.
Ainda não consegui abrir em nenhum navegador, mais pelo menos consegui gerar o arquivo.
Abaixo vai o código que fiz para gerar esse arquivo.

Vale resaltar que usei uma estratégia de "buffers" para conseguir chegar nesse resultado.
Para quem tiver idéias melhores, mais performaticas, ou outra solução posta aqui.
Para os que tiverem interesse em analisar o modo que construi o código, fazer perguntas, estou a disposição para respondêlas.

Outra coisa que vale resaltar: String em Java é um Pé no Saco! :x

Cores.java
package cores;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Cores {

	public static String toHex(Long i) {
		String hex = Integer.toHexString(i.intValue());
		while (hex.length() <= 5) {
			hex = "0" + hex;
		}
		return hex;
	}

	public static void main(String[] args) throws Exception {
		Date dtUm = new Date();

		List<StringBuilder> buffers = new ArrayList<StringBuilder>();
		StringBuilder cores = null;
		boolean flag = true;
		long num = 0;
		while (flag) {
			cores = new StringBuilder();
			try {
				for (; num <= 16777215; num++) {
					cores.append("<>#" + toHex(num) + "</>\n");
				}
				flag = false;
				buffers.add(cores);
			} catch (OutOfMemoryError e) {
				System.out.println("Volta: " + num);
				System.out.println("Ultima cor:  #" + toHex(num));
				buffers.add(cores);
			} catch (Exception e) {
				e.printStackTrace();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}

		BufferedWriter fw = new BufferedWriter(new FileWriter(new File("C:/cores2.html"), true));
		int size = buffers.size();
		System.out.println("Total buffers: " + size);

		int tentativas = 0;
		for (int s = 0; s < size; s++) {
			try {
				StringBuilder tcores = buffers.get(s);
				int lenght = tcores.length();
				int total = 0;
				while (total != lenght) {
					total += 10000;
					int dif = 0;
					if (total > lenght) {
						dif = total - lenght;
						total = lenght;
					}
					fw.append(tcores.substring(total - 10000 + dif, total));
				}
				tentativas = 0;
			} catch (OutOfMemoryError e) {
				if (tentativas <= 5) {
					System.out.println("Buffer: " + s);
					System.out.println("Tentativa: " + tentativas);
					tentativas++;
					s--;
				} else {
					tentativas = 0;
				}
			}
		}
		fw.close();

		File file = new File("C:/cores2.html");
		File file2 = new File("C:/cores.html");

		BufferedReader br = new BufferedReader(new FileReader(file));
		BufferedWriter bw = new BufferedWriter(new FileWriter(file2, true));

		String str = null;
		boolean first = true;
		while (str != null || first) {
			str = br.readLine();
			if (str != null) {
				str = str.replace("<>", "<div style=\"background:");
				str = str.replace("</>", "\">_</div>");
				bw.append(str);
			}
			first = false;
		}
		br.close();
		bw.close();

		file.delete();

		Date dtDois = new Date();
		System.out.println("Tempo total: " + (dtDois.getTime() - dtUm.getTime()) + " milisegundos");
	}

}
Criado 28 de outubro de 2010
Respostas 0
Participantes 1