← Back to team overview

indiv-screenlets-dev team mailing list archive

[Merge] lp:~doctormo/indiv-screenlets/color-changing-ringsensor into lp:indiv-screenlets

 

Martin Owens has proposed merging lp:~doctormo/indiv-screenlets/color-changing-ringsensor into lp:indiv-screenlets.

Requested reviews:
  Individual Screenlets Developers (indiv-screenlets-dev)

For more details, see:
https://code.launchpad.net/~doctormo/indiv-screenlets/color-changing-ringsensor/+merge/55178

Adds some new colour options into the ring screenlet. Fancy and awesome.
-- 
https://code.launchpad.net/~doctormo/indiv-screenlets/color-changing-ringsensor/+merge/55178
Your team Individual Screenlets Developers is requested to review the proposed merge of lp:~doctormo/indiv-screenlets/color-changing-ringsensor into lp:indiv-screenlets.
=== modified file 'src/RingSensors/RingSensorsScreenlet.py'
--- src/RingSensors/RingSensorsScreenlet.py	2011-02-23 23:36:12 +0000
+++ src/RingSensors/RingSensorsScreenlet.py	2011-03-28 16:18:30 +0000
@@ -17,7 +17,8 @@
 
 import screenlets
 from screenlets import sensors
-from screenlets.options import FloatOption, BoolOption, StringOption, IntOption, ColorOption
+from screenlets.options import FloatOption, BoolOption, StringOption,\
+    IntOption, ColorOption, ColorsOption
 import cairo
 import pango
 import sys
@@ -57,8 +58,9 @@
 	linear = ''
 	hourMiddle = False
 	color_back = (1,1,1,0.6)
-	color_front =(1, 1, 1, 0.9)
+	color_front = (1, 1, 1, 0.9)
 	color_text = (1, 1, 1, 1)
+	style_front = 'Fan'
 	graph_type = 'Graph'
 	thickness = 24.0
 	ringSpacing = 26.0
@@ -71,7 +73,7 @@
 	new_cpu = 0
 	wire_list = []
 	wire_data = []
-	big_bars = True
+	segment_count = 12
 
 	down = 0
 	download = 0
@@ -139,16 +141,23 @@
 		self.add_option(ColorOption(_('Sensors'),'color_back', 
 			self.color_back, 'background color', ''))
 
-		self.add_option(ColorOption(_('Sensors'),'color_front', 
-			self.color_front, _('Front color'), 
-			''))
+		self.add_option(ColorsOption(_('Sensors'),'color_front',
+			default=self.color_front, label=_('Active Color')))
+
+		self.add_option(StringOption(_('Sensors'),'style_front',
+			default=self.style_front, label=_('Active Style'),
+			choices=['Fan', 'Grad']))
+
 		self.add_option(ColorOption(_('Sensors'),'color_text', 
 			self.color_text, _('Text color'), 
 			''))
 		self.add_option(FloatOption(_('Sensors'), 'thickness', self.thickness, _('Ring Thickness'), '',min=0.0, max=500.0, increment=0.1))
 		self.add_option(FloatOption(_('Sensors'), 'blockSpacing', self.blockSpacing, _('Block Spacing'), '',min=0.0, max=6.0, increment=0.1))
-		self.add_option(BoolOption(_('Sensors'), 'big_bars',
-			self.big_bars, _('Use Big bars'), ''))
+
+		self.add_option(IntOption(_('Sensors'), 'segment_count',
+			default=self.segment_count, label=_('Number of Segments'),
+			desc=_('Big Bars is 12, Small is 60'), min=10, max=80))
+
 		self.add_option(FloatOption(_('Sensors'), 'max_download', self.max_download, _('Max dl speed (kilobytes/s)'), '',min=0.0, max=1000000.0, increment=10))
 		self.add_option(FloatOption(_('Sensors'), 'max_upload', self.max_upload, _('Max ul speed (kilobytes/s)'), '',min=0.0, max=1000000.0, increment=10))
 
@@ -278,54 +287,75 @@
 		self.redraw_canvas()
 		return True
 
+	def gradiant_colours(self, colours, index, max):
+		"""Take a set of colours and make a pretty rainbow."""
+		# These first checks provide compatability and stop wasteful
+		# Mathamatics when we know the outcome.
+		if type(colours[0]) in [float, int]:
+			return colours
+		if len(colours) == 1:
+			return colours[0]
+		if index == max:
+			return colours[-1]
+
+		grad = float(index) / max
+		isect = len(colours) - 1
+		sect = int(grad * isect)
+		grad = (float(index) - (max / isect * sect)) / (max / isect)
+		first = colours[sect]
+		second = colours[sect+1]
+		return (
+			first[0] + (second[0] - first[0]) * grad,
+			first[1] + (second[1] - first[1]) * grad,
+			first[2] + (second[2] - first[2]) * grad,
+			first[3] + (second[3] - first[3]) * grad,
+		)
 
 	def on_draw(self, ctx):
-
+		"""Draw a rainbow coloured arc as the sensor value."""
 		ctx.scale(self.scale, self.scale)
 		# draw bg (if theme available)
 		ctx.set_operator(cairo.OPERATOR_ADD)
-		
-
-		startrad = self.size-(self.thickness/2.0)		
-		a = float(self.load*11)/100
-				
-		
-		ctx.set_line_width( self.thickness )	
-		if self.big_bars:
-			for i in range(12):
-				if i == a or (i<=a ): col = self.color_front
-				else: col = self.color_back
-				if self.hourMiddle: radius = startrad-(self.ringSpacing*2.0)
-				else: radius = startrad
-				pos = -90+(self.blockSpacing/2.0)+(i*30)
-				
-				ctx.arc( 100, 100, radius, math.radians(pos), math.radians(pos+30-self.blockSpacing) )
-				ctx.set_source_rgba(col[0],col[1],col[2],col[3])
-				ctx.stroke()
-		else:
-			a = float(self.load*59)/100
-			for i in range(60):
-				if i == a or (i<=a ): col = self.color_front
-				else: col = self.color_back
-				if self.hourMiddle: radius = startrad-(self.ringSpacing*2.0)
-				else: radius = startrad
-				pos = -90+(self.blockSpacing/2.0)+(i*6)
-				
-				ctx.arc( 100, 100, radius, math.radians(pos), math.radians(pos+6-self.blockSpacing) )
-				ctx.set_source_rgba(col[0],col[1],col[2],col[3])
-				ctx.stroke()
+
+		startrad = self.size-(self.thickness/2.0)
+		ctx.set_line_width( self.thickness )
+
+		# This could now be made into a more variable option.
+		segments = self.segment_count
+		fract = float(self.load * (segments-1)) / 100
+
+		col = (0, 0, 0, 1)
+		if self.style_front == 'Fan':
+			col = self.gradiant_colours(self.color_front, fract, segments - 1)
+
+		for i in range(segments):
+			if i > fract:
+				col = self.color_back
+			elif self.style_front == 'Grad':
+				col = self.gradiant_colours(self.color_front, i, segments - 1)
+
+			if self.hourMiddle:
+				radius = startrad-(self.ringSpacing*2.0)
+			else:
+				radius = startrad
+
+			rad_width = 360 / segments
+			pos = -90 + (self.blockSpacing / 2.0) + (i * rad_width)
+			rad_from = math.radians(pos)
+			rad_to = math.radians(pos + rad_width - self.blockSpacing)
+			ctx.arc( 100, 100, radius, rad_from, rad_to )
+			ctx.set_source_rgba(col[0], col[1], col[2], col[3])
+			ctx.stroke()
+
 		if len(str(self.load))==1:
 			self.load = "0" + str(self.load)
 		ctx.set_source_rgba(self.color_text[0],self.color_text[1],self.color_text[2],self.color_text[3])
 		if self.sensor.endswith('RPM') or self.sensor.endswith('C') or self.sensor.endswith('V') or self.sensor.find(':') != -1:
-			text = '<small><small><small><small>' +str(self.sensor.split(':')[0]) +'</small></small></small></small>\n'+str(self.sensor.split(':')[1])	
+			text = '<small><small><small><small>' +str(self.sensor.split(':')[0]) +'</small></small></small></small>\n'+str(self.sensor.split(':')[1])
 		else:
 			text = '<small><small><small><small>' +self.sensor +'</small></small></small></small>\n'+self.text_prefix + str(self.load) + self.text_suffix
 		ctx.set_operator(cairo.OPERATOR_OVER)
 		if self.show_text and self.theme:self.theme.draw_text(ctx,text, 0, 70, 'Free Sans', 25,  self.width,pango.ALIGN_CENTER)
-			
-			
-	
 
 	def on_draw_shape(self,ctx):
 		self.on_draw(ctx)


Follow ups