예제 #1
0
 def do_scn(self):
     try:
         r, g, b = self.pop(self.ncs.ncomponents)
         r, g, b = rint(r * 255), rint(g * 255), rint(b * 255)
         self.ncs.color = '#%02x%02x%02x' % (r, g, b)
     except:
         pass
예제 #2
0
def plouffe(f, epsilon=1e-6):
    if f < 0:
        r = plouffe(-f)
        if isinstance(r, str):
            return '-' + r
        return f
    if f != 0 and math2.is_integer(1 / f, epsilon):
        f = '1/%d' % math2.rint(1 / f)
    elif math2.is_integer(f * f, epsilon):
        f = 'sqrt(%d)' % math2.rint(f * f)
    return f
예제 #3
0
파일: expr.py 프로젝트: goulu/Goulib
def plouffe(f, epsilon=1e-6):
    if f < 0:
        r = plouffe(-f)
        if isinstance(r, str):
            return '-' + r
        return f
    if f != 0 and math2.is_integer(1 / f, epsilon):
        f = '1/%d' % math2.rint(1 / f)
    elif math2.is_integer(f * f, epsilon):
        f = 'sqrt(%d)' % math2.rint(f * f)
    return f
예제 #4
0
파일: colors.py 프로젝트: goulu/Goulib
 def pil(self):
     """
     :return: a sequence of integers, or a string containing a binary 
     representation of the palette.
     In both cases, the palette contents should be ordered (r, g, b, r, g, b, …). 
     The palette can contain up to 768 entries (3*256). 
     If a shorter palette is given, it is padded with zeros.
     #http://effbot.org/zone/creating-palette-images.htm
     """
     res=[]
     for c in self.values():
         r,g,b=c.rgb
         res.append(math2.rint(r*255))
         res.append(math2.rint(g*255))
         res.append(math2.rint(b*255))
     return res
예제 #5
0
 def pil(self):
     """
     :return: a sequence of integers, or a string containing a binary 
     representation of the palette.
     In both cases, the palette contents should be ordered (r, g, b, r, g, b, …). 
     The palette can contain up to 768 entries (3*256). 
     If a shorter palette is given, it is padded with zeros.
     #http://effbot.org/zone/creating-palette-images.htm
     """
     res = []
     for c in self.values():
         r, g, b = c.rgb
         res.append(math2.rint(r * 255))
         res.append(math2.rint(g * 255))
         res.append(math2.rint(b * 255))
     return res
예제 #6
0
파일: image.py 프로젝트: goulu/Goulib
def disk(radius, antialias=PILImage.ANTIALIAS):
    from skimage.draw import circle, circle_perimeter_aa
    size = math2.rint(2 * radius)
    size = (size, size)
    img = np.zeros(size, dtype=np.double)
    rr, cc = circle(radius, radius, radius)
    img[rr, cc] = 1
    # antialiased perimeter works only with ints ?
    """
    rr, cc, val = circle_perimeter_aa(radius,radius,radius)
    img[rr, cc] = val
    """
    return Image(img)
예제 #7
0
def timef(t,fmt='%H:%M:%S'):
    '''converts something to a time. See datetimef'''
    if isinstance(t,dt.datetime):
        return t.time()
    if isinstance(t,dt.time):
        return t
    if isinstance(t,(six.integer_types,float)):
        if not '%d' in fmt: # t is in hours
            s=math2.rint(t*3600)
            h,m=divmod(s,3600)
            m,s=divmod(m,60)
            return time(hour=h,minute=m,second=s)
        else: # t is in days (Excel
            pass
    return datetimef(t,fmt=fmt).time()
예제 #8
0
def timef(t, fmt='%H:%M:%S'):
    '''converts something to a time. See datetimef'''
    if isinstance(t, dt.datetime):
        return t.time()
    if isinstance(t, dt.time):
        return t
    if isinstance(t, (six.integer_types, float)):
        if not '%d' in fmt:  # t is in hours
            s = math2.rint(t * 3600)
            h, m = divmod(s, 3600)
            m, s = divmod(m, 60)
            return time(hour=h, minute=m, second=s)
        else:  # t is in days (Excel
            pass
    return datetimef(t, fmt=fmt).time()
예제 #9
0
    def __init__(self, f, context=default_context):
        '''
        :param f: function or operator, Expr to copy construct, or formula string
        '''
        self.context = context

        if isinstance(f, Expr):  # copy constructor
            self.body = f.body
            return
        elif isinstance(f, ast.AST):
            self.body = f
            return
        elif inspect.isfunction(f):
            try:
                f = get_function_source(f)
            except ValueError:
                f = '%s(x)' % f.__name__
        elif isinstance(f, collections.Callable):  # builtin function
            f = '%s(x)' % f.__name__
        elif f in ('True', 'False'):
            f = bool(f == 'True')

        if type(f) is bool:
            self.body = ast.Num(f)
            return

        if type(f) is float:  # try to beautify it
            if math2.is_integer(f):
                f = math2.rint(f)
            else:
                f = plouffe(f)

        if math2.is_number(f):  # store it with full precision
            self.body = ast.Num(f)
            return

        # accept ^ as power operator rather than xor ...
        f = str(f).replace('^', '**')

        self.body = compile(f, 'Expr', 'eval', ast.PyCF_ONLY_AST).body
예제 #10
0
파일: expr.py 프로젝트: goulu/Goulib
    def __init__(self, f, context=default_context):
        '''
        :param f: function or operator, Expr to copy construct, or formula string
        '''
        self.context = context

        if isinstance(f, Expr):  # copy constructor
            self.body = f.body
            return
        elif isinstance(f, ast.AST):
            self.body = f
            return
        elif inspect.isfunction(f):
            try:
                f = get_function_source(f)
            except ValueError:
                f = '%s(x)' % f.__name__
        elif isinstance(f, collections.Callable):  # builtin function
            f = '%s(x)' % f.__name__
        elif f in ('True', 'False'):
            f = bool(f == 'True')

        if type(f) is bool:
            self.body = ast.Num(f)
            return

        if type(f) is float:  # try to beautify it
            if math2.is_integer(f):
                f = math2.rint(f)
            else:
                f = plouffe(f)

        if math2.is_number(f):  # store it with full precision
            self.body = ast.Num(f)
            return

        f = str(f).replace('^', '**')  # accept ^ as power operator rather than xor ...

        self.body = compile(f, 'Expr', 'eval', ast.PyCF_ONLY_AST).body
예제 #11
0
 def do_sc(self):
     r, g, b = self.pop(self.scs.ncomponents)
     r, g, b = rint(r * 255), rint(g * 255), rint(b * 255)
     self.scs.color = '#%02x%02x%02x' % (r, g, b)
예제 #12
0
 def do_RG(self, r, g, b):
     from pdfminer.pdfcolor import LITERAL_DEVICE_RGB
     self.do_CS(LITERAL_DEVICE_RGB)
     r, g, b = rint(r * 255), rint(g * 255), rint(b * 255)
     self.scs.color = '#%02x%02x%02x' % (r, g, b)