paint_app.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import random
  2. import sys
  3. from PyQt5 import QtWidgets, QtGui, QtCore
  4. COLORS = [
  5. # 17undertoneshttps://lospec.com/palette-list/17undertones
  6. '#000000',
  7. '#141923',
  8. '#414168',
  9. '#3A7FA7',
  10. '#35E3E3',
  11. '#8FD970',
  12. '#5EBB49',
  13. '#458352',
  14. '#DCD37B',
  15. '#FFFEE5',
  16. '#FFD035',
  17. '#CC9245',
  18. '#A15C3E',
  19. '#A42F3B',
  20. '#F45B7A',
  21. '#C24998',
  22. '#81588D',
  23. '#BCB0C2',
  24. '#FFFFFF',
  25. ]
  26. SPRAY_PARTICLES = 100
  27. SPRAY_DIAMMETER = 10
  28. class Canvas(QtWidgets.QLabel):
  29. def __init__(self):
  30. super().__init__()
  31. pixmap = QtGui.QPixmap(600, 300)
  32. self.setPixmap(pixmap)
  33. self.last_x, self.last_y = None, None
  34. self.pen_color = QtGui.QColor('#000000')
  35. def set_pen_color(self, c):
  36. self.pen_color = QtGui.QColor(c)
  37. def mouseMoveEvent(self, e):
  38. if self.last_x is None:
  39. self.last_x = e.x()
  40. self.last_y = e.y()
  41. return
  42. painter = QtGui.QPainter(self.pixmap())
  43. p = painter.pen()
  44. p.setWidth(1)
  45. p.setColor(self.pen_color)
  46. painter.setPen(p)
  47. for n in range(SPRAY_PARTICLES):
  48. xo = random.gauss(0, SPRAY_DIAMMETER)
  49. yo = random.gauss(0, SPRAY_DIAMMETER)
  50. painter.drawPoint(e.x() + xo, e.y() + yo)
  51. self.update()
  52. self.last_x = e.x()
  53. self.last_y = e.y()
  54. def mouseReleaseEvent(self, e):
  55. self.last_x, self.last_y = None, None
  56. class QPaletteButton(QtWidgets.QPushButton):
  57. def __init__(self, color):
  58. super().__init__()
  59. self.setFixedSize(QtCore.QSize(24, 24))
  60. self.color = color
  61. self.setStyleSheet("background-color: %s;" % color)
  62. class MainWindow(QtWidgets.QMainWindow):
  63. def __init__(self):
  64. super().__init__()
  65. self.canvas = Canvas()
  66. w = QtWidgets.QWidget()
  67. l = QtWidgets.QVBoxLayout()
  68. w.setLayout(l)
  69. l.addWidget(self.canvas)
  70. palette = QtWidgets.QHBoxLayout()
  71. self.add_palette_buttons(palette)
  72. l.addLayout(palette)
  73. self.setCentralWidget(w)
  74. def add_palette_buttons(self, layout):
  75. for c in COLORS:
  76. b = QPaletteButton(c)
  77. b.pressed.connect(lambda color=c: self.canvas.set_pen_color(color))
  78. layout.addWidget(b)
  79. if __name__ == '__main__':
  80. app = QtWidgets.QApplication(sys.argv)
  81. window = MainWindow()
  82. window.show()
  83. app.exec_()