<commit_before>"""Color Database.

To create a class that contains color lookup methods, use the module global
function `get_colordb(file)'.  This function will try to examine the file to
figure out what the format of the file is.  If it can't figure out the file
format, or it has trouble reading the file, None is returned.  You can pass
get_colordb() an optional filetype argument.

Supporte file types are:

    X_RGB_TXT -- X Consortium rgb.txt format files.  Three columns of numbers
                 from 0 .. 255 separated by whitespace.  Arbitrary trailing
                 columns used as the color name.
"""

import sys
import string
import re
from types import *
import operator

class BadColor(Exception):
    pass

DEFAULT_DB = None




# generic class
class ColorDB:
    def __init__(self, fp, lineno):
	# Maintain several dictionaries for indexing into the color database.
	# Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits, 
	# for now we only support 8 bit intensities.  At least on OpenWindows, 
	# all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
	#
	# key is (red, green, blue) tuple, value is (name, [aliases])
	self.__byrgb = {}
	#
	# key is name, value is (red, green, blue)
	self.__byname = {}
	#
	while 1:
	    line = fp.readline()
	    if not line:
		break
	    # get this compiled regular expression from derived class
	    mo = self._re.match(line)
	    if not mo:
		sys.stderr.write('Error in %s, line %d\n' % (fp.name, lineno))
		lineno = lineno + 1
		continue
	    #
	    # extract the red, green, blue, and name
	    #
	    red, green, blue = map(int, mo.group('red', 'green', 'blue'))
	    name = mo.group('name')
	    keyname = string.lower(name)
	    #
	    # TBD: for now the `name' is just the first named color with the
	    # rgb values we find.  Later, we might want to make the two word
	    # version the `name', or the CapitalizedVersion, etc.
	    #
	    key = (red, green, blue)
	    foundname, aliases = self.__byrgb.get(key, (name, []))
	    if foundname <> name and foundname not in aliases:
		aliases.append(name)
	    self.__byrgb[key] = (foundname, aliases)
	    #
	    # add to byname lookup
	    #
	    self.__byname[keyname] = key
	    lineno = lineno + 1

    def find_byrgb(self, rgbtuple):
	try:
	    return self.__byrgb[rgbtuple]
	except KeyError:
	    raise BadColor(rgbtuple)

    def find_byname(self, name):
	name = string.lower(name)
	try:
	    return self.__byname[name]
	except KeyError:
	    raise BadColor(name)

    def nearest(self, red, green, blue):
	# TBD: use Voronoi diagrams, Delaunay triangulation, or octree for
	# speeding up the locating of nearest point.  Exhaustive search is
	# inefficient, but may be fast enough.
	nearest = -1
	nearest_name = ''
	for name, aliases in self.__byrgb.values():
	    r, g, b = self.__byname[string.lower(name)]
	    rdelta = red - r
	    gdelta = green - g
	    bdelta = blue - b
	    distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta
	    if nearest == -1 or distance < nearest:
		nearest = distance
		nearest_name = name
	return nearest_name
	


class RGBColorDB(ColorDB):
    _re = re.compile(
	'\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)')




# format is a tuple (RE, SCANLINES, CLASS) where RE is a compiled regular<commit_msg>all_names(): Return just the unique names of rgb colors<commit_after>"""Color Database.

To create a class that contains color lookup methods, use the module global
function `get_colordb(file)'.  This function will try to examine the file to
figure out what the format of the file is.  If it can't figure out the file
format, or it has trouble reading the file, None is returned.  You can pass
get_colordb() an optional filetype argument.

Supporte file types are:

    X_RGB_TXT -- X Consortium rgb.txt format files.  Three columns of numbers
                 from 0 .. 255 separated by whitespace.  Arbitrary trailing
                 columns used as the color name.
"""

import sys
import string
import re
from types import *
import operator

class BadColor(Exception):
    pass

DEFAULT_DB = None




# generic class
class ColorDB:
    def __init__(self, fp, lineno):
	# Maintain several dictionaries for indexing into the color database.
	# Note that while Tk supports RGB intensities of 4, 8, 12, or 16 bits, 
	# for now we only support 8 bit intensities.  At least on OpenWindows, 
	# all intensities in the /usr/openwin/lib/rgb.txt file are 8-bit
	#
	# key is (red, green, blue) tuple, value is (name, [aliases])
	self.__byrgb = {}
	#
	# key is name, value is (red, green, blue)
	self.__byname = {}
	#
        # all unique names (non-aliases).  built-on demand
        self.__allnames = None
	while 1:
	    line = fp.readline()
	    if not line:
		break
	    # get this compiled regular expression from derived class
	    mo = self._re.match(line)
	    if not mo:
		sys.stderr.write('Error in %s, line %d\n' % (fp.name, lineno))
		lineno = lineno + 1
		continue
	    #
	    # extract the red, green, blue, and name
	    #
	    red, green, blue = map(int, mo.group('red', 'green', 'blue'))
	    name = mo.group('name')
	    keyname = string.lower(name)
	    #
	    # TBD: for now the `name' is just the first named color with the
	    # rgb values we find.  Later, we might want to make the two word
	    # version the `name', or the CapitalizedVersion, etc.
	    #
	    key = (red, green, blue)
	    foundname, aliases = self.__byrgb.get(key, (name, []))
	    if foundname <> name and foundname not in aliases:
		aliases.append(name)
	    self.__byrgb[key] = (foundname, aliases)
	    #
	    # add to byname lookup
	    #
	    self.__byname[keyname] = key
	    lineno = lineno + 1

    def find_byrgb(self, rgbtuple):
	try:
	    return self.__byrgb[rgbtuple]
	except KeyError:
	    raise BadColor(rgbtuple)

    def find_byname(self, name):
	name = string.lower(name)
	try:
	    return self.__byname[name]
	except KeyError:
	    raise BadColor(name)

    def nearest(self, red, green, blue):
	# TBD: use Voronoi diagrams, Delaunay triangulation, or octree for
	# speeding up the locating of nearest point.  Exhaustive search is
	# inefficient, but may be fast enough.
	nearest = -1
	nearest_name = ''
	for name, aliases in self.__byrgb.values():
	    r, g, b = self.__byname[string.lower(name)]
	    rdelta = red - r
	    gdelta = green - g
	    bdelta = blue - b
	    distance = rdelta * rdelta + gdelta * gdelta + bdelta * bdelta
	    if nearest == -1 or distance < nearest:
		nearest = distance
		nearest_name = name
	return nearest_name

    def all_names(self):
        # sorted
        if not self.__allnames:
            self.__allnames = []
            for name, aliases in self.__byrgb.values():
                self.__allnames.append(name)
            self.__allnames.sort()
        return self.__allnames
	


class RGBColorDB(ColorDB):
    _re = re.compile(
	'\s*(?P<red>\d+)\s+(?P<green>\d+)\s+(?P<blue>\d+)\s+(?P<name>.*)')




# format is a tuple (RE, SCANLINES, CLASS) where RE is a compiled regular
{'fraction_non_alphanumeric': 0.1008952678698309, 'fraction_numerical': 0.004831604376865141, 'mean_word_length': 2.8480043739748497, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1841338', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2240, 'n_words': 827}