Drag and drop colors

  1. 8 months ago

    Gijs R

    12 Apr 2019 Rotterdam, Netherlands

    Hi everyone, is there a way to drag and drop color within a Xojo app. Or drag and drop a color from the system wide color picker and a Xojo app?

  2. Alexander v

    12 Apr 2019 Europe (Houten, The Netherland...
    Edited 8 months ago

    Hi Gijs,

    that is possible.

    Assuming that the color to drag from and to is in a screen object like a Rectangle.
    Create a MouseDown event in the FROM rectangle:

    Function MouseDown(X As Integer, Y As Integer) Handles MouseDown as Boolean
      Dim d As DragItem 'create a dragitem
      d=New DragItem(Me,X,Y,Me.width,Me.height) 'give it the same dimensions as the FROM object
      d.Text = ColorToString(Me.FillColor) ' fill it with the current color string
      
      d.Drag  'allow the drag
    End Function

    In the TO Rectangle create an Open event:

    Sub Open() Handles Open
      me.AcceptTextDrop 'allow text drop
    End Sub

    and a DropObject event:

    Sub DropObject(obj As DragItem, action As Integer) Handles DropObject
      Me.FillColor = StringToColor(obj.Text) 'read the colorstring and convert it
    End Sub

    Color from/to string conversions:

    Public Function ColorToString(aColor as Color) as String
        Return "#" + aColor.Red.ToHex + aColor.Green.ToHex + aColor.Blue.ToHex
      End Function
    Public Function StringToColor(theColor as String) as Color
      Dim v As Variant = theColor.Replace("#", "&c")
      Dim c As Color = v.ColorValue
      Return c
      End Function

    You can now drag and drop the color. I have a small try-out App if you want.

    Cheers!

  3. Gijs R

    12 Apr 2019 Rotterdam, Netherlands

    @Alexander vnbsp;der Linden - Thanks! This works great for dragging colors between objects (tried rectangles as you suggested).
    Is there also a way to include the System Wide Apple color palette in draging and dropping colors?

  4. Alexander v

    12 Apr 2019 Europe (Houten, The Netherland...

    Check SelectColor()

  5. 4 months ago

    Is there any idea on this one? I can get a color from SelectColor(), I can create a color from scratch color.rgba(...)
    but I can't seem to locate a color in a drop from the system color picker.

    I created an app, added a canvas and configured it to accept drops i.e. me.AcceptRawDataDrop("public.item")

    I can drag a color from the color picker to my canvas, and it triggers the DropObject event, but...
    obj.RawDataAvailable("public.item") is false. I don't know how to get the color from the dragItem that was dropped.

    Any ideas what the UTI is that is used? (i searched the apple UTI doc and none match)

    Thanks

  6. Emile S

    Aug 13 Europe (France, Strasbourg)
    Edited 4 months ago

    David: from where do you make the Color Picker appears “above the Canvas” ?

    Can you do that in - say - LibreOffice ?

    Edit: Do the Color Picker meant to be used that way ?
    Not with Apple’s TextEdit (with the Color Picker displaying Pens). NO: THIS WORKS (but you have to have a text selected) !

    But this have no sense: a simple click is enough to get the color.

  7. Emile S

    Aug 13 Europe (France, Strasbourg)

    Why?

    The LR example:
    Dim c As Color Dim b As Boolean c = CMY(0.35, 0.9, 0.6) // choose the default color shown in color picker b = SelectColor(c, "Select a Color")

    c holds the selected color. You have to use it (g.ForeColor = c) and draw something: it will be done with the just selected color.

    Do you have troubles with that code ?

  8. Emile,
    Thanks for your comments.
    I can certainly handle dragging and dropping colors purely within my own program, the system color picker allows you to maintain a pallet of colors you've used. You can drag those to apps which use the color picker (like textEdit yes)

    It seems reasonable to be able to drag a color to my app from the color picker too. but without knowing the UTI, I can't get the data from the dragItem.

    That is all I was hoping that someone knew the trick for.

  9. Emile S

    Aug 13 Europe (France, Strasbourg)

    This is your project. You have a solution.

    @David A someone knew the trick for

    ?

  10. Norman P

    Aug 13 Pre-Release Testers, Xojo Pro Outside Innisfail

    @David A It seems reasonable to be able to drag a color to my app from the color picker too. but without knowing the UTI, I can't get the data from the dragItem.

    com.app.cocoa.pasteboard.color
    the drag pasteboard seems to have a binary plist on it that specifies the color

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    	<key>$archiver</key>
    	<string>NSKeyedArchiver</string>
    	<key>$objects</key>
    	<array>
    		<string>$null</string>
    		<dict>
    			<key>$class</key>
    			<dict>
    				<key>CF$UID</key>
    				<integer>6</integer>
    			</dict>
    			<key>NSColorSpace</key>
    			<integer>1</integer>
    			<key>NSComponents</key>
    			<data>
    			MC42MzAzMDI3ODUgMC42MzAzMDI3ODUgMC42MzAzMDI3ODUgMQ==
    			</data>
    			<key>NSCustomColorSpace</key>
    			<dict>
    				<key>CF$UID</key>
    				<integer>2</integer>
    			</dict>
    			<key>NSRGB</key>
    			<data>
    			MC41NjI1NjgzNjY1IDAuNTYyNTUxNDM4OCAwLjU2MjU2MDk3NTYA
    			</data>
    		</dict>
    		<dict>
    			<key>$class</key>
    			<dict>
    				<key>CF$UID</key>
    				<integer>5</integer>
    			</dict>
    			<key>NSICC</key>
    			<dict>
    				<key>CF$UID</key>
    				<integer>3</integer>
    			</dict>
    			<key>NSID</key>
    			<integer>7</integer>
    		</dict>
    		<dict>
    			<key>$class</key>
    			<dict>
    				<key>CF$UID</key>
    				<integer>4</integer>
    			</dict>
    			<key>NS.data</key>
    			<data>
    			AAAMSExpbm8CEAAAbW50clJHQiBYWVogB84AAgAJAAYAMQAAYWNz
    			cE1TRlQAAAAASUVDIHNSR0IAAAAAAAAAAAAAAAAAAPbWAAEAAAAA
    			0y1IUCAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    			AAAAAAAAAAAAAAAAAAARY3BydAAAAVAAAAAzZGVzYwAAAYQAAABs
    			d3RwdAAAAfAAAAAUYmtwdAAAAgQAAAAUclhZWgAAAhgAAAAUZ1hZ
    			WgAAAiwAAAAUYlhZWgAAAkAAAAAUZG1uZAAAAlQAAABwZG1kZAAA
    			AsQAAACIdnVlZAAAA0wAAACGdmlldwAAA9QAAAAkbHVtaQAAA/gA
    			AAAUbWVhcwAABAwAAAAkdGVjaAAABDAAAAAMclRSQwAABDwAAAgM
    			Z1RSQwAABDwAAAgMYlRSQwAABDwAAAgMdGV4dAAAAABDb3B5cmln
    			aHQgKGMpIDE5OTggSGV3bGV0dC1QYWNrYXJkIENvbXBhbnkAAGRl
    			c2MAAAAAAAAAEnNSR0IgSUVDNjE5NjYtMi4xAAAAAAAAAAAAAAAS
    			c1JHQiBJRUM2MTk2Ni0yLjEAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    			AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAADz
    			UQABAAAAARbMWFlaIAAAAAAAAAAAAAAAAAAAAABYWVogAAAAAAAA
    			b6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAA
    			ACSgAAAPhAAAts9kZXNjAAAAAAAAABZJRUMgaHR0cDovL3d3dy5p
    			ZWMuY2gAAAAAAAAAAAAAABZJRUMgaHR0cDovL3d3dy5pZWMuY2gA
    			AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    			AAAAAAAAZGVzYwAAAAAAAAAuSUVDIDYxOTY2LTIuMSBEZWZhdWx0
    			IFJHQiBjb2xvdXIgc3BhY2UgLSBzUkdCAAAAAAAAAAAAAAAuSUVD
    			IDYxOTY2LTIuMSBEZWZhdWx0IFJHQiBjb2xvdXIgc3BhY2UgLSBz
    			UkdCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGRlc2MAAAAAAAAALFJl
    			ZmVyZW5jZSBWaWV3aW5nIENvbmRpdGlvbiBpbiBJRUM2MTk2Ni0y
    			LjEAAAAAAAAAAAAAACxSZWZlcmVuY2UgVmlld2luZyBDb25kaXRp
    			b24gaW4gSUVDNjE5NjYtMi4xAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    			AAAAAAB2aWV3AAAAAAATpP4AFF8uABDPFAAD7cwABBMLAANcngAA
    			AAFYWVogAAAAAABMCVYAUAAAAFcf521lYXMAAAAAAAAAAQAAAAAA
    			AAAAAAAAAAAAAAAAAAKPAAAAAnNpZyAAAAAAQ1JUIGN1cnYAAAAA
    			AAAEAAAAAAUACgAPABQAGQAeACMAKAAtADIANwA7AEAARQBKAE8A
    			VABZAF4AYwBoAG0AcgB3AHwAgQCGAIsAkACVAJoAnwCkAKkArgCy
    			ALcAvADBAMYAywDQANUA2wDgAOUA6wDwAPYA+wEBAQcBDQETARkB
    			HwElASsBMgE4AT4BRQFMAVIBWQFgAWcBbgF1AXwBgwGLAZIBmgGh
    			AakBsQG5AcEByQHRAdkB4QHpAfIB+gIDAgwCFAIdAiYCLwI4AkEC
    			SwJUAl0CZwJxAnoChAKOApgCogKsArYCwQLLAtUC4ALrAvUDAAML
    			AxYDIQMtAzgDQwNPA1oDZgNyA34DigOWA6IDrgO6A8cD0wPgA+wD
    			+QQGBBMEIAQtBDsESARVBGMEcQR+BIwEmgSoBLYExATTBOEE8AT+
    			BQ0FHAUrBToFSQVYBWcFdwWGBZYFpgW1BcUF1QXlBfYGBgYWBicG
    			NwZIBlkGagZ7BowGnQavBsAG0QbjBvUHBwcZBysHPQdPB2EHdAeG
    			B5kHrAe/B9IH5Qf4CAsIHwgyCEYIWghuCIIIlgiqCL4I0gjnCPsJ
    			EAklCToJTwlkCXkJjwmkCboJzwnlCfsKEQonCj0KVApqCoEKmAqu
    			CsUK3ArzCwsLIgs5C1ELaQuAC5gLsAvIC+EL+QwSDCoMQwxcDHUM
    			jgynDMAM2QzzDQ0NJg1ADVoNdA2ODakNww3eDfgOEw4uDkkOZA5/
    			DpsOtg7SDu4PCQ8lD0EPXg96D5YPsw/PD+wQCRAmEEMQYRB+EJsQ
    			uRDXEPURExExEU8RbRGMEaoRyRHoEgcSJhJFEmQShBKjEsMS4xMD
    			EyMTQxNjE4MTpBPFE+UUBhQnFEkUahSLFK0UzhTwFRIVNBVWFXgV
    			mxW9FeAWAxYmFkkWbBaPFrIW1hb6Fx0XQRdlF4kXrhfSF/cYGxhA
    			GGUYihivGNUY+hkgGUUZaxmRGbcZ3RoEGioaURp3Gp4axRrsGxQb
    			OxtjG4obshvaHAIcKhxSHHscoxzMHPUdHh1HHXAdmR3DHeweFh5A
    			HmoelB6+HukfEx8+H2kflB+/H+ogFSBBIGwgmCDEIPAhHCFIIXUh
    			oSHOIfsiJyJVIoIiryLdIwojOCNmI5QjwiPwJB8kTSR8JKsk2iUJ
    			JTglaCWXJccl9yYnJlcmhya3JugnGCdJJ3onqyfcKA0oPyhxKKIo
    			1CkGKTgpaymdKdAqAio1KmgqmyrPKwIrNitpK50r0SwFLDksbiyi
    			LNctDC1BLXYtqy3hLhYuTC6CLrcu7i8kL1ovkS/HL/4wNTBsMKQw
    			2zESMUoxgjG6MfIyKjJjMpsy1DMNM0YzfzO4M/E0KzRlNJ402DUT
    			NU01hzXCNf02NzZyNq426TckN2A3nDfXOBQ4UDiMOMg5BTlCOX85
    			vDn5OjY6dDqyOu87LTtrO6o76DwnPGU8pDzjPSI9YT2hPeA+ID5g
    			PqA+4D8hP2E/oj/iQCNAZECmQOdBKUFqQaxB7kIwQnJCtUL3QzpD
    			fUPARANER0SKRM5FEkVVRZpF3kYiRmdGq0bwRzVHe0fASAVIS0iR
    			SNdJHUljSalJ8Eo3Sn1KxEsMS1NLmkviTCpMcky6TQJNSk2TTdxO
    			JU5uTrdPAE9JT5NP3VAnUHFQu1EGUVBRm1HmUjFSfFLHUxNTX1Oq
    			U/ZUQlSPVNtVKFV1VcJWD1ZcVqlW91dEV5JX4FgvWH1Yy1kaWWlZ
    			uFoHWlZaplr1W0VblVvlXDVchlzWXSddeF3JXhpebF69Xw9fYV+z
    			YAVgV2CqYPxhT2GiYfViSWKcYvBjQ2OXY+tkQGSUZOllPWWSZedm
    			PWaSZuhnPWeTZ+loP2iWaOxpQ2maafFqSGqfavdrT2una/9sV2yv
    			bQhtYG25bhJua27Ebx5veG/RcCtwhnDgcTpxlXHwcktypnMBc11z
    			uHQUdHB0zHUodYV14XY+dpt2+HdWd7N4EXhueMx5KnmJeed6Rnql
    			ewR7Y3vCfCF8gXzhfUF9oX4BfmJ+wn8jf4R/5YBHgKiBCoFrgc2C
    			MIKSgvSDV4O6hB2EgITjhUeFq4YOhnKG14c7h5+IBIhpiM6JM4mZ
    			if6KZIrKizCLlov8jGOMyo0xjZiN/45mjs6PNo+ekAaQbpDWkT+R
    			qJIRknqS45NNk7aUIJSKlPSVX5XJljSWn5cKl3WX4JhMmLiZJJmQ
    			mfyaaJrVm0Kbr5wcnImc951kndKeQJ6unx2fi5/6oGmg2KFHobai
    			JqKWowajdqPmpFakx6U4pammGqaLpv2nbqfgqFKoxKk3qamqHKqP
    			qwKrdavprFys0K1ErbiuLa6hrxavi7AAsHWw6rFgsdayS7LCsziz
    			rrQltJy1E7WKtgG2ebbwt2i34LhZuNG5SrnCuju6tbsuu6e8Ibyb
    			vRW9j74KvoS+/796v/XAcMDswWfB48JfwtvDWMPUxFHEzsVLxcjG
    			RsbDx0HHv8g9yLzJOsm5yjjKt8s2y7bMNcy1zTXNtc42zrbPN8+4
    			0DnQutE80b7SP9LB00TTxtRJ1MvVTtXR1lXW2Ndc1+DYZNjo2WzZ
    			8dp22vvbgNwF3IrdEN2W3hzeot8p36/gNuC94UThzOJT4tvjY+Pr
    			5HPk/OWE5g3mlucf56noMui86Ubp0Opb6uXrcOv77IbtEe2c7iju
    			tO9A78zwWPDl8XLx//KM8xnzp/Q09ML1UPXe9m32+/eK+Bn4qPk4
    			+cf6V/rn+3f8B/yY/Sn9uv5L/tz/bf//
    			</data>
    		</dict>
    		<dict>
    			<key>$classes</key>
    			<array>
    				<string>NSMutableData</string>
    				<string>NSData</string>
    				<string>NSObject</string>
    			</array>
    			<key>$classname</key>
    			<string>NSMutableData</string>
    		</dict>
    		<dict>
    			<key>$classes</key>
    			<array>
    				<string>NSColorSpace</string>
    				<string>NSObject</string>
    			</array>
    			<key>$classname</key>
    			<string>NSColorSpace</string>
    		</dict>
    		<dict>
    			<key>$classes</key>
    			<array>
    				<string>NSColor</string>
    				<string>NSObject</string>
    			</array>
    			<key>$classname</key>
    			<string>NSColor</string>
    		</dict>
    	</array>
    	<key>$top</key>
    	<dict>
    		<key>root</key>
    		<dict>
    			<key>CF$UID</key>
    			<integer>1</integer>
    		</dict>
    	</dict>
    	<key>$version</key>
    	<integer>100000</integer>
    </dict>
    </plist>

or Sign Up to reply!