Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.38 KB | None | 0 0
  1. from PyQt5.QtWidgets import QWidget, QApplication, QColorDialog, QPushButton, QVBoxLayout, QGridLayout, QLabel, \
  2.     QLineEdit, QSizePolicy
  3. import sys
  4.  
  5.  
  6. class Window(QWidget):
  7.     def __init__(self, parent=None):
  8.         super().__init__(parent)
  9.  
  10.         self.red = 0
  11.         self.green = 0
  12.         self.blue = 0
  13.         self.cyan = 0
  14.         self.magenta = 0
  15.         self.yellow = 0
  16.         self.black = 0
  17.  
  18.         self.red_channel_input = QLineEdit(self)
  19.         self.green_channel_input = QLineEdit(self)
  20.         self.blue_channel_input = QLineEdit(self)
  21.         self.cyan_channel_input = QLineEdit(self)
  22.         self.magenta_channel_input = QLineEdit(self)
  23.         self.yellow_channel_input = QLineEdit(self)
  24.         self.black_channel_input = QLineEdit(self)
  25.  
  26.         self.red_channel_input.setReadOnly(True)
  27.         self.green_channel_input.setReadOnly(True)
  28.         self.blue_channel_input.setReadOnly(True)
  29.         self.cyan_channel_input.setReadOnly(True)
  30.         self.magenta_channel_input.setReadOnly(True)
  31.         self.yellow_channel_input.setReadOnly(True)
  32.         self.black_channel_input.setReadOnly(True)
  33.  
  34.         self.red_channel_input.setStyleSheet("background-color: darkGray")
  35.         self.green_channel_input.setStyleSheet("background-color: darkGray")
  36.         self.blue_channel_input.setStyleSheet("background-color: darkGray")
  37.         self.cyan_channel_input.setStyleSheet("background-color: darkGray")
  38.         self.magenta_channel_input.setStyleSheet("background-color: darkGray")
  39.         self.yellow_channel_input.setStyleSheet("background-color: darkGray")
  40.         self.black_channel_input.setStyleSheet("background-color: darkGray")
  41.  
  42.         self.color_button = QPushButton()
  43.         self.color_button.setSizePolicy(
  44.             QSizePolicy.Preferred,
  45.             QSizePolicy.Expanding)
  46.  
  47.         self.interface()
  48.  
  49.     def interface(self):
  50.         pick_color = QPushButton("Pick a color", self)
  51.         # draw_cube = QPushButton("Draw a 3D cube", self)
  52.         convert_to_rgb = QPushButton("Convert from CMYK to RGB", self)
  53.         convert_to_cmyk = QPushButton("Convert from RGB to CMYK", self)
  54.  
  55.         pick_color.clicked.connect(self.color_picker)
  56.         convert_to_rgb.clicked.connect(self.cmyk_to_rgb)
  57.         convert_to_cmyk.clicked.connect(self.rgb_to_cmyk)
  58.  
  59.         box_layout = QVBoxLayout()
  60.  
  61.         box_layout.addWidget(pick_color)
  62.         # box_layout.addWidget(draw_cube)
  63.         box_layout.addWidget(convert_to_rgb)
  64.         box_layout.addWidget(convert_to_cmyk)
  65.  
  66.         grid_layout = QGridLayout()
  67.         grid_layout.addLayout(box_layout, 1, 1)
  68.  
  69.         input_grid_layout = QGridLayout()
  70.         grid_layout.addLayout(input_grid_layout, 0, 1)
  71.  
  72.         red_label = QLabel("R:", self)
  73.         green_label = QLabel("G:", self)
  74.         blue_label = QLabel("B:", self)
  75.         cyan_label = QLabel("C:", self)
  76.         magenta_label = QLabel("M:", self)
  77.         yellow_label = QLabel("Y:", self)
  78.         black_label = QLabel("K:", self)
  79.  
  80.         red_box = QVBoxLayout()
  81.         red_box.addWidget(red_label)
  82.         red_box.addWidget(self.red_channel_input)
  83.  
  84.         green_box = QVBoxLayout()
  85.         green_box.addWidget(green_label)
  86.         green_box.addWidget(self.green_channel_input)
  87.  
  88.         blue_box = QVBoxLayout()
  89.         blue_box.addWidget(blue_label)
  90.         blue_box.addWidget(self.blue_channel_input)
  91.  
  92.         cyan_box = QVBoxLayout()
  93.         cyan_box.addWidget(cyan_label)
  94.         cyan_box.addWidget(self.cyan_channel_input)
  95.  
  96.         magenta_box = QVBoxLayout()
  97.         magenta_box.addWidget(magenta_label)
  98.         magenta_box.addWidget(self.magenta_channel_input)
  99.  
  100.         yellow_box = QVBoxLayout()
  101.         yellow_box.addWidget(yellow_label)
  102.         yellow_box.addWidget(self.yellow_channel_input)
  103.  
  104.         black_box = QVBoxLayout()
  105.         black_box.addWidget(black_label)
  106.         black_box.addWidget(self.black_channel_input)
  107.  
  108.         input_grid_layout.addLayout(red_box, 0, 1)
  109.         input_grid_layout.addLayout(green_box, 0, 2)
  110.         input_grid_layout.addLayout(blue_box, 0, 3)
  111.         input_grid_layout.addLayout(cyan_box, 1, 1)
  112.         input_grid_layout.addLayout(magenta_box, 1, 2)
  113.         input_grid_layout.addLayout(yellow_box, 1, 3)
  114.         input_grid_layout.addLayout(black_box, 1, 4)
  115.         input_grid_layout.addWidget(self.color_button, 0, 0)
  116.  
  117.         self.setLayout(grid_layout)
  118.  
  119.         self.setGeometry(760, 440, 200, 200)
  120.         self.setWindowTitle("Zadanie3")
  121.  
  122.     @staticmethod
  123.     def get_rgb_string(red, green, blue):
  124.         return "rgb({}, {}, {})".format(red, green, blue)
  125.  
  126.     def color_picker(self):
  127.         color = QColorDialog.getColor()
  128.  
  129.         self.red = color.red()
  130.         self.green = color.green()
  131.         self.blue = color.blue()
  132.         self.cyan = self.scale_down_color_channel(color.cyan())
  133.         self.magenta = self.scale_down_color_channel(color.magenta())
  134.         self.yellow = self.scale_down_color_channel(color.yellow())
  135.         self.black = self.scale_down_color_channel(color.black())
  136.  
  137.         self.color_button.setStyleSheet("background-color: {}".format(self.get_rgb_string(self.red, self.green, self.blue)))
  138.  
  139.         self.red_channel_input.clear()
  140.         self.green_channel_input.clear()
  141.         self.blue_channel_input.clear()
  142.  
  143.         self.cyan_channel_input.clear()
  144.         self.magenta_channel_input.clear()
  145.         self.yellow_channel_input.clear()
  146.         self.black_channel_input.clear()
  147.  
  148.     def rgb_to_cmyk(self):
  149.         red = self.scale_down_color_channel(self.red)
  150.         green = self.scale_down_color_channel(self.green)
  151.         blue = self.scale_down_color_channel(self.blue)
  152.  
  153.         black_channel = min(1 - red, 1 - green, 1 - blue)
  154.  
  155.         if black_channel == 1:
  156.             cyan_channel = 0
  157.             magenta_channel = 0
  158.             yellow_channel = 0
  159.         else:
  160.             cyan_channel = (1 - red - black_channel) / (1 - black_channel)
  161.             magenta_channel = (1 - green - black_channel) / (1 - black_channel)
  162.             yellow_channel = (1 - blue - black_channel) / (1 - black_channel)
  163.  
  164.         self.cyan = cyan_channel
  165.         self.magenta = magenta_channel
  166.         self.yellow = yellow_channel
  167.         self.black = black_channel
  168.  
  169.         self.cyan_channel_input.setText(str(round(self.cyan, 2)))
  170.         self.magenta_channel_input.setText(str(round(self.magenta, 2)))
  171.         self.yellow_channel_input.setText(str(round(self.yellow, 2)))
  172.         self.black_channel_input.setText(str(round(self.black, 2)))
  173.  
  174.     def cmyk_to_rgb(self):
  175.         self.red = self.scale_up_color_channel(1 - min(1, (self.cyan * (1 - self.black)) + self.black))
  176.         self.green = self.scale_up_color_channel(1 - min(1, (self.magenta * (1 - self.black)) + self.black))
  177.         self.blue = self.scale_up_color_channel(1 - min(1, (self.yellow * (1 - self.black)) + self.black))
  178.  
  179.         self.red_channel_input.setText(str(round(self.red, 2)))
  180.         self.green_channel_input.setText(str(round(self.green, 2)))
  181.         self.blue_channel_input.setText(str(round(self.blue, 2)))
  182.  
  183.     @staticmethod
  184.     def scale_down_color_channel(color_channel):
  185.         return color_channel / 255
  186.  
  187.     @staticmethod
  188.     def scale_up_color_channel(color_channel):
  189.         return color_channel * 255
  190.  
  191.  
  192. def main():
  193.     app = QApplication(sys.argv)
  194.     window = Window()
  195.     window.show()
  196.  
  197.     sys.exit(app.exec_())
  198.  
  199.  
  200. if __name__ == "__main__":
  201.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement