本文整理汇总了Python中qgis.PyQt.QtGui.QGradient类的典型用法代码示例。如果您正苦于以下问题:Python QGradient类的具体用法?Python QGradient怎么用?Python QGradient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QGradient类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testQgsVectorGradientRampV2
#.........这里部分代码省略.........
s = r.stops()
self.assertEqual(len(s), 1)
self.assertEqual(s[0].offset, 0.4)
self.assertEqual(s[0].color, QColor(100, 100, 40))
# test info
r.setInfo({'key1': 'val1', 'key2': 'val2'})
self.assertEqual(r.info()['key1'], 'val1')
self.assertEqual(r.info()['key2'], 'val2')
# test creating from properties
props = r.properties()
fromProps = QgsGradientColorRamp.create(props)
self.assertEqual(fromProps.color1(), QColor(0, 0, 200))
self.assertEqual(fromProps.color2(), QColor(0, 0, 100))
s = fromProps.stops()
self.assertEqual(len(s), 1)
self.assertEqual(s[0].offset, 0.4)
c = QColor(s[0].color)
self.assertEqual(c, QColor(100, 100, 40))
self.assertEqual(fromProps.info()['key1'], 'val1')
self.assertEqual(fromProps.info()['key2'], 'val2')
self.assertEqual(fromProps.isDiscrete(), False)
# test cloning ramp
cloned = r.clone()
self.assertEqual(cloned.color1(), QColor(0, 0, 200))
self.assertEqual(cloned.color2(), QColor(0, 0, 100))
s = cloned.stops()
self.assertEqual(len(s), 1)
self.assertEqual(s[0].offset, 0.4)
c = QColor(s[0].color)
self.assertEqual(c, QColor(100, 100, 40))
self.assertEqual(cloned.info()['key1'], 'val1')
self.assertEqual(cloned.info()['key2'], 'val2')
self.assertEqual(cloned.isDiscrete(), False)
# test discrete ramps
# first with no stops
d = QgsGradientColorRamp(QColor(200, 0, 0), QColor(0, 200, 0), True)
self.assertEqual(d.isDiscrete(), True)
self.assertEqual(d.color(0), QColor(200, 0, 0))
self.assertEqual(d.color(0.5), QColor(200, 0, 0))
self.assertEqual(d.color(1), QColor(0, 200, 0))
# then with stops
d = QgsGradientColorRamp(QColor(200, 0, 0), QColor(0, 200, 0), True, [QgsGradientStop(0.1, QColor(180, 20, 40)),
QgsGradientStop(0.9, QColor(40, 60, 100))])
self.assertEqual(d.isDiscrete(), True)
self.assertEqual(d.color(0), QColor(200, 0, 0))
self.assertEqual(d.color(0.05), QColor(200, 0, 0))
self.assertEqual(d.color(0.1), QColor(180, 20, 40))
self.assertEqual(d.color(0.5), QColor(180, 20, 40))
self.assertEqual(d.color(0.9), QColor(40, 60, 100))
self.assertEqual(d.color(0.95), QColor(40, 60, 100))
self.assertEqual(d.color(1), QColor(0, 200, 0))
# to gradient
g = QGradient()
r = QgsGradientColorRamp(QColor(200, 0, 0), QColor(0, 200, 0), False, [QgsGradientStop(0.1, QColor(180, 20, 40)),
QgsGradientStop(0.9, QColor(40, 60, 100))])
r.addStopsToGradient(g, 0.5)
self.assertEqual(len(g.stops()), 4)
self.assertEqual(g.stops()[0], (0.0, QColor(200, 0, 0, 127)))
self.assertEqual(g.stops()[1], (0.1, QColor(180, 20, 40, 127)))
self.assertEqual(g.stops()[2], (0.9, QColor(40, 60, 100, 127)))
self.assertEqual(g.stops()[3], (1.0, QColor(0, 200, 0, 127)))
# test that stops are ordered when setting them
# first add some out-of-order stops
r.setStops([QgsGradientStop(0.4, QColor(100, 100, 40)),
QgsGradientStop(0.2, QColor(200, 200, 80)),
QgsGradientStop(0.8, QColor(50, 20, 10)),
QgsGradientStop(0.6, QColor(10, 10, 4))])
s = r.stops()
self.assertEqual(len(s), 4)
self.assertEqual(s[0].offset, 0.2)
self.assertEqual(s[0].color, QColor(200, 200, 80))
self.assertEqual(s[1].offset, 0.4)
self.assertEqual(s[1].color, QColor(100, 100, 40))
self.assertEqual(s[2].offset, 0.6)
self.assertEqual(s[2].color, QColor(10, 10, 4))
self.assertEqual(s[3].offset, 0.8)
self.assertEqual(s[3].color, QColor(50, 20, 10))
# test continuous invert function
r.invert()
self.assertEqual(r.color(0), QColor(0, 200, 0))
self.assertEqual(r.color(1), QColor(200, 0, 0))
self.assertEqual(r.color(0.2), QColor(50, 20, 10))
# test discrete invert function
r = QgsGradientColorRamp(QColor(255, 255, 255), QColor(0, 0, 0), True, [QgsGradientStop(0.33, QColor(128, 128, 128)),
QgsGradientStop(0.66, QColor(0, 0, 0))])
self.assertEqual(r.color(0.2), QColor(255, 255, 255))
self.assertEqual(r.color(0.5), QColor(128, 128, 128))
self.assertEqual(r.color(0.8), QColor(0, 0, 0))
r.invert()
self.assertEqual(r.color(0.2), QColor(0, 0, 0))
self.assertEqual(r.color(0.5), QColor(128, 128, 128))
self.assertEqual(r.color(0.8), QColor(255, 255, 255))
示例2: testQgsVectorGradientRampV2
#.........这里部分代码省略.........
self.assertEqual(r.value(0), 0.0)
self.assertEqual(r.value(1), 1.0)
self.assertEqual(r.color(0), QColor(200, 0, 0, 100))
self.assertEqual(r.color(1), QColor(0, 200, 0, 200))
self.assertEqual(r.color(0.5), QColor(100, 100, 0, 150))
# test gradient with stops
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0), False, [QgsGradientStop(0.1, QColor(180, 20, 40)),
QgsGradientStop(0.9, QColor(40, 60, 100))])
self.assertEqual(r.color1(), QColor(200, 0, 0))
self.assertEqual(r.color2(), QColor(0, 200, 0))
self.assertEqual(r.isDiscrete(), False)
self.assertEqual(len(r.stops()), 2)
self.assertEqual(r.count(), 4)
self.assertEqual(r.value(0), 0.0)
self.assertEqual(r.value(1), 0.1)
self.assertEqual(r.value(2), 0.9)
self.assertEqual(r.value(3), 1.0)
self.assertEqual(r.color(0), QColor(200, 0, 0))
self.assertEqual(r.color(0.05), QColor(190, 10, 20))
self.assertEqual(r.color(0.1), QColor(180, 20, 40))
self.assertEqual(r.color(0.5), QColor(110, 40, 70))
self.assertEqual(r.color(0.9), QColor(40, 60, 100))
self.assertEqual(r.color(0.95), QColor(20, 130, 50))
self.assertEqual(r.color(1), QColor(0, 200, 0))
# test setters
r.setColor1(QColor(0, 0, 200))
self.assertEqual(r.color1(), QColor(0, 0, 200))
self.assertEqual(r.color(0), QColor(0, 0, 200))
r.setColor2(QColor(0, 0, 100))
self.assertEqual(r.color2(), QColor(0, 0, 100))
self.assertEqual(r.color(1.0), QColor(0, 0, 100))
r.setStops([QgsGradientStop(0.4, QColor(100, 100, 40))])
s = r.stops()
self.assertEqual(len(s), 1)
self.assertEqual(s[0].offset, 0.4)
self.assertEqual(s[0].color, QColor(100, 100, 40))
# test info
r.setInfo({'key1': 'val1', 'key2': 'val2'})
self.assertEqual(r.info()['key1'], 'val1')
self.assertEqual(r.info()['key2'], 'val2')
# test creating from properties
props = r.properties()
fromProps = QgsVectorGradientColorRampV2.create(props)
self.assertEqual(fromProps.color1(), QColor(0, 0, 200))
self.assertEqual(fromProps.color2(), QColor(0, 0, 100))
s = fromProps.stops()
self.assertEqual(len(s), 1)
self.assertEqual(s[0].offset, 0.4)
c = QColor(s[0].color)
self.assertEqual(c, QColor(100, 100, 40))
self.assertEqual(fromProps.info()['key1'], 'val1')
self.assertEqual(fromProps.info()['key2'], 'val2')
self.assertEqual(fromProps.isDiscrete(), False)
# test cloning ramp
cloned = r.clone()
self.assertEqual(cloned.color1(), QColor(0, 0, 200))
self.assertEqual(cloned.color2(), QColor(0, 0, 100))
s = cloned.stops()
self.assertEqual(len(s), 1)
self.assertEqual(s[0].offset, 0.4)
c = QColor(s[0].color)
self.assertEqual(c, QColor(100, 100, 40))
self.assertEqual(cloned.info()['key1'], 'val1')
self.assertEqual(cloned.info()['key2'], 'val2')
self.assertEqual(cloned.isDiscrete(), False)
# test discrete ramps
# first with no stops
d = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0), True)
self.assertEqual(d.isDiscrete(), True)
self.assertEqual(d.color(0), QColor(200, 0, 0))
self.assertEqual(d.color(0.5), QColor(200, 0, 0))
self.assertEqual(d.color(1), QColor(0, 200, 0))
# then with stops
d = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0), True, [QgsGradientStop(0.1, QColor(180, 20, 40)),
QgsGradientStop(0.9, QColor(40, 60, 100))])
self.assertEqual(d.isDiscrete(), True)
self.assertEqual(d.color(0), QColor(200, 0, 0))
self.assertEqual(d.color(0.05), QColor(200, 0, 0))
self.assertEqual(d.color(0.1), QColor(180, 20, 40))
self.assertEqual(d.color(0.5), QColor(180, 20, 40))
self.assertEqual(d.color(0.9), QColor(40, 60, 100))
self.assertEqual(d.color(0.95), QColor(40, 60, 100))
self.assertEqual(d.color(1), QColor(0, 200, 0))
# to gradient
g = QGradient()
r = QgsVectorGradientColorRampV2(QColor(200, 0, 0), QColor(0, 200, 0), False, [QgsGradientStop(0.1, QColor(180, 20, 40)),
QgsGradientStop(0.9, QColor(40, 60, 100))])
r.addStopsToGradient(g, 0.5)
self.assertEqual(len(g.stops()), 4)
self.assertEqual(g.stops()[0], (0.0, QColor(200, 0, 0, 127)))
self.assertEqual(g.stops()[1], (0.1, QColor(180, 20, 40, 127)))
self.assertEqual(g.stops()[2], (0.9, QColor(40, 60, 100, 127)))
self.assertEqual(g.stops()[3], (1.0, QColor(0, 200, 0, 127)))