-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverloadingoprator.py
55 lines (41 loc) · 1.43 KB
/
overloadingoprator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-10-29 18:19:46
# @Author : jingray ([email protected])
# @Link : http://www.jianshu.com/u/01fb0364467d
# @Version : $Id$
import matplotlib.pyplot as plt
'''
The color class creates a color from 3 values, r, g, and b (red, green, and blue).
attributes:
r - a value between 0-255 for red
g - a value between 0-255 for green
b - a value between 0-255 for blue
'''
class Color(object):
# Initializes a color with rgb values
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
# Called when a Color object is printed out
def __repr__(self):
'''Display a color swatch and returns a text description of r,g,b values'''
plt.imshow([[(self.r/255, self.g/255, self.b/255)]])
return 'r, g, b = ' + str(self.r) + ', ' + str(self.g) + ', ' + str(self.b)
## TODO: Complete this add function to add two colors together
def __add__(self, other):
'''Adds the r, g, and b components of each color together
and averaging them.
The new Color object, with these averaged rgb values,
is returned.'''
self.r = (self.r+other.r)/2
self.g = (self.g+other.g)/2
self.b = (self.b+other.b)/2
return self
color1 = color.Color(250, 0, 0)
print(color1)
color2 = color.Color(0, 50, 200)
print(color2)
new_color = color1 + color2
print(new_color)